Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change z-index temporarily on hover?

I have several products overlapping eachother with z-index.

I've tried this.

$(document).on('mouseover', '.product', function(e) { 
    prod_id = $(this).attr('id');               //Each product has an id
    prod_zindex = $(this).css('z-index');       //Store z-index when hovered
    $(this).css('z-index',50000000);
});

//Restore z-index when moving from product
$(document).on('mouseout', '.product', function(e) { 
    $('#' + prod_id ).css('z-index',prod_zindex);        
});

prod_id and prod_zindex are global variables

What I WANT to happen is that the hovered product should be set to highest z-index, but when I move away from that I want the changed z-index to be restored to it's original.

Obviously I'm doing something wrong. I guess it's because I can't control in which order the mouseover/mouseout - events are going to be executed.

What would be the best approach to handle this scenario? The "original" state of the products are like z-index=0, z-index=1, z-index=2 etc.

Please give me any hint/pointers...

UPDATE

Clarification: In normal case I could use .product:hover in css, but in this case it will have no effect because z-indexes are set inline on each and every product (and it has to be that way)

like image 786
bestprogrammerintheworld Avatar asked Mar 14 '14 09:03

bestprogrammerintheworld


People also ask

Can I change Z-Index on hover?

Go to states, click to hover… change background, add box shadow, give a transform to scale 1.02, go to z-index add 5. That would set your hover so you can test it directly on canvas.

Does Z-Index work on static?

z-index only affects elements that have a position value other than static (the default). Elements can overlap for a variety of reasons, for instance, relative positioning has nudged it over something else. Negative margin has pulled the element over another. Absolutely positioned elements overlap each other.

What is Z-Index Auto?

z-index: auto. z-index: auto. For a positioned box (that is, one with any position other than static ), the z-index property specifies: The stack level of the box in the current stacking context. Whether the box establishes a local stacking context.

Why Z-index is not working in before?

z-index only works on positioned elements (relative, absolute, fixed, sticky) so if you set a z-index on an element with a static position, it won't work.


1 Answers

why dont you use simpe css for that?

.product:hover{ 
  z-index:555555;
}

It will change the z-index only on hover and return back to its previous state when mouse moves out.

Edit

  .product:hover
   {
     z-index:555555 !important;
   }
like image 101
Anoop Joshi P Avatar answered Oct 22 '22 03:10

Anoop Joshi P