Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. 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.

like image 634
Rakesh Juyal Avatar asked Oct 13 '10 12:10

Rakesh Juyal


People also ask

What is Z index in jQuery?

The z-index specifies the order of stack for an element. The element which has the highest order of stack always appear in front of the element which have the lowest order of stack. The z-index uses on positioned elements like position: absolute, position: relative, position: sticky or position: fixed.

How do you set Z index dynamically?

css("z-index")); if ( isNaN(currentIndex) ) { // if is not a number, set it to 0 currentIndex = 0; } var num = currentIndex++; $(this). parent(". ui-wrapper"). css("z-index", num ); });

How do you use Z index in Javascript?

z-index:1200px; top:450px; position:absolute; right:300px; document. getElementById('iframe_div1'). style.


1 Answers

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.

like image 82
BrunoLM Avatar answered Oct 04 '22 03:10

BrunoLM