jQuery 的 $('#divOne').animate({zIndex: -1000}, 2000) 不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3122926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery's $('#divOne').animate({zIndex: -1000}, 2000) does not work?
提问by nonopolarity
I tried jQuery's
我试过jQuery的
$('#divOne').animate({zIndex: -1000}, 2000)
to that element which has a z-index of 1000, but it is still above the other elements?
到 z-index 为 1000 的元素,但它仍然高于其他元素?
(If I use firebug to change it to -1000
then it will be below other elements)
(如果我使用 firebug 将其更改为-1000
那么它将低于其他元素)
回答by James
jQuery attempts to add a unit to the value on each step of the animation. So, instead of 99
it'll be 99px
which, of course, isn't a valid zIndex
value.
jQuery 尝试为动画的每个步骤的值添加一个单位。所以,而不是99
它99px
,当然,不是一个有效的zIndex
值。
It doesn't seem possible to set the unit used by jQuery to simply a blank string -- it'll either take the unit you include in the value (e.g. 20%
- percent unit) or it will use px
.
似乎不可能将 jQuery 使用的单位设置为简单的空白字符串——它要么采用您包含在值中的单位(例如20%
- 百分比单位),要么使用px
.
Fortunately, you can hack animate()
to make this work:
幸运的是,您可以通过 hackanimate()
来完成这项工作:
var div = $('#divOne');
$({
z: ~~div.css('zIndex')
// ~~ to get an integer, even from non-numerical values like "auto"
}).animate({
z: -1000
}, {
step: function() {
div.css('zIndex', ~~this.z);
},
duration: 2000
});
For more info about ~~
see this.
有关更多信息,~~
请参阅此。