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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:53:56  来源:igfitidea点击:

jQuery's $('#divOne').animate({zIndex: -1000}, 2000) does not work?

jqueryz-indexjquery-animate

提问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 -1000then 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 99it'll be 99pxwhich, of course, isn't a valid zIndexvalue.

jQuery 尝试为动画的每个步骤的值添加一个单位。所以,而不是9999px,当然,不是一个有效的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.

有关更多信息,~~请参阅

回答by redsquare

You cannot animate the zindex.You can set it using .css.

您不能为 zindex 设置动画。您可以使用.css设置它。

$("#divOne").css('z-index' , '-1000');

$("#divOne").css('z-index' , '-1000');