简体   繁体   English

为什么我无法使用jquery设置di​​v的z-index?

[英]Why i am unable to set z-index of div using jquery?

I have created a sample 我创建了一个样本
http://jsbin.com/eqiti3 here we have three divs. http://jsbin.com/eqiti3这里我们有三个div。 Now, what i want to do is: on clicking of any div it should come on the top of other div then fade and then back to its original position. 现在,我想要做的是:点击任何div它应该在其他div的顶部然后淡出然后回到它原来的位置。 This is what i am using: 这就是我正在使用的:

$(".box").click( function () {
    var zindex = $(this).css('z-index');     
  /* this too is not working
    $(this).css('z-index',14);  
    $(this).fadeTo ( 'slow', 0.5 ).fadeTo('slow', 1 )
    $(this).css('z-index',zindex);
  */

    $(this).css('z-index',14).fadeTo  ( 'slow', 0.5 ).fadeTo('slow', 1 ).css('z-index',zindex);

} );

provided $(this).css('z-index',14) this alone is capable of making the div to come over other divs. 提供$(this).css('z-index',14)这个单独能够让div来过其他div。

Use the callback 使用回调

$(this).css('z-index',14)
    .fadeTo('slow', 0.5 )
    .fadeTo('slow', 1, function(){
        $(this).css('z-index',zindex);
    });

The 3rd parameter is a callback function , it will run when the animation ends. 第三个参数是回调函数 ,它将在动画结束时运行。

Revision #3 on jsBin . 关于jsBin的修订版#3

change your code to this: 将您的代码更改为:

$(".box").click( function () {
   var zindex = $(this).css('z-index');     

   $(this).css('z-index',14).fadeTo  ( 'slow', 0.5 ).fadeTo('slow', 1, function(){
       $(this).css('z-index',zindex);
   });
});

You can't chain the .css() method after fadeTo() , because those fx functions run asyncronously and therefore, .css() was executing immediately. 你不能在fadeTo()之后fadeTo() .css()方法,因为那些fx函数是异步运行的,因此, .css()立即执行。

That's why all fx methods do offer callbacks which fire when finished. 这就是为什么所有fx方法都提供了在完成时触发的回调。

See this in action: http://jsbin.com/eqiti3/2/edit 请参阅此操作: http//jsbin.com/eqiti3/2/edit

Set background:'white'; 设置背景:'白色'; it is solved for me 它为我解决了

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

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