简体   繁体   English

$ .css('transition','value')不起作用

[英]$.css('transition','value') doesn't work

I'm trying to apply css3 transition on the fly, but jquery doesn't do anything. 我正在尝试动态应用css3过渡,但jquery却无能为力。

code

var test = $("#test");
test.css("transition","all 500ms");

i also tried 我也尝试过

var delay = "500ms";

test.css({
    'transition': 'all ' + delay,
    '-moz-transition': 'all ' + delay,
    '-webkit-transition': 'all ' + delay,
    '-o-transition': 'all ' + delay
});

What's wrong with my code? 我的代码有什么问题?

Updated: 更新:

"jquery doesn't do anything" literally means that it doesn't apply style to the element. “ jquery不会做任何事情”从字面上意味着它不会将样式应用于元素。 I don't mean that the element doesn't move. 我并不是说该元素不会移动。

Correction: 更正:

it actually works...it was my browser. 它确实有效...这是我的浏览器。

I don't think you understand how CSS3 transitions work. 我认为您不了解CSS3过渡的工作原理。 The CSS3 transition settings control how property changes to an object are handled and whether they take immediate effect or whether they trigger a transition so it transtions to the new value over time. CSS3过渡设置控制如何处理对象的属性更改以及它们是否立即生效或是否触发过渡,以便随着时间的推移过渡到新值。

You must have an actual property change to have a transition. 您必须进行实际的属性更改才能进行过渡。 Setting the transition value itself does not trigger a transition - it just sets up the rules for when a transition will occur in the future. 设置过渡值本身不会触发过渡-只是为将来何时发生过渡设置规则。

In your example, you'd have to actually change some value to see a change. 在您的示例中,您必须实际更改一些值才能看到更改。 For example: 例如:

test.css("height", "100px");

Since you have transition: all , that would trigger a transition from the current height value to the new height value. 由于您具有transition: all ,这将触发从当前高度值到新高度值的过渡。

For example, here's a CSS3 transition I created for another question a couple days ago: http://jsfiddle.net/jfriend00/P2H4Z/ . 例如,这是几天前我为另一个问题创建的CSS3过渡: http : //jsfiddle.net/jfriend00/P2H4Z/

In that example, there is this CSS relevant to the transition: 在该示例中,存在与此转换相关的CSS:

.element
{
    background: blue;
    position: absolute;
    left: -100%;
    padding: 0 10px;

    -moz-transition: left 1s; 
    -webkit-transition: left 1s; 
    -o-transition: left 1s; 
    -ms-transition: left 1s; 
     transition: left 1s; 
}


.element.seen {
    left: 0;
}

This sets the stage for a transition of the left property. 这为left属性的过渡奠定了基础。 It does not trigger a transition. 它不会触发过渡。 The transition can be triggered by actually making a change to the left property on the item with class="element" . 可以通过实际更改class="element"项的left属性来触发转换。 That change to the left value can be done by: 可以通过以下方式完成对left值的更改:

  1. Adding a class to the item or a parent item which causes a different left value to take effect from the CSS rules. 将一个类添加到该项目或父项目,这会导致不同的left值从CSS规则生效。
  2. Triggering a pseudo class to take effect (like :hover ) 触发伪类生效(例如:hover
  3. Programmatically changing the value of .left with javascript. 使用javascript以编程方式更改.left的值。

In this particular example, the seen class is added to the object which causes the left value to change from -100% to 0 . 在此特定示例中,将seen类添加到对象,这将导致左值从-100%更改为0 Because transition left 1s is defined, the browser will transition from the current -100% value to the new 0 value over a duration of 1s and thus the object will slide into place. 由于定义了transition left 1s ,因此浏览器将在1s的时间内从当前的-100%值过渡到新的0值,因此对象将滑入到位。

var delay = "500ms",
    test  = $("#test");

test.css({
    'transition': 'all ' + delay,
    '-moz-transition': 'all ' + delay,
    '-webkit-transition': 'all ' + delay,
    '-o-transition': 'all ' + delay
}).on('click', function () {
    $(this).css({ left : '+=100' });
});​

http://jsfiddle.net/YwfjF/ http://jsfiddle.net/YwfjF/

You can see that it works, but you have to change a property for it to be animated. 您可以看到它起作用,但是必须更改属性才能使其具有动画效果。

Here is a great reference for CSS3 transitions: https://developer.mozilla.org/en/CSS/transition (notice the links at the bottom that describe the different parts of the transition declaration). 这是CSS3过渡的绝佳参考: https : //developer.mozilla.org/en/CSS/transition (请注意底部的链接,该链接描述了过渡声明的不同部分)。

Step 1: define a css class for transitions: 步骤1:为过渡定义CSS类:

.CustomTransitionClass{
    -webkit-transition:all 0.5s ease;
    -moz-transition:all 0.5s ease;
    -o-transition:all 0.5s ease;
    transition:all 0.5s ease;}

Step 2: add the class: 步骤2:添加课程:

 $("#test").addClass('CustomTransitionClass');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM