Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery hover problem due to z-index

I want to trigger a hover event for an element using jQuery, but I have an semi-transparent png positioned over the element using z-index. Is there any way to tell jQuery to ignore the png and trigger the hover event for the element underneath it?

like image 569
user424616 Avatar asked Aug 18 '10 23:08

user424616


People also ask

Does Z index affect hover?

If an element is transparent and the element with z-index:-1; is 'under' it. This stops the hover effects. Z-index can you see as elevations in a building, and you watching it from birdseye. You can't reach the basement if there is a floor above it, even if its build from glass.

What does zindex do?

The z-index CSS property sets the z-order of a positioned element and its descendants or flex items. Overlapping elements with a larger z-index cover those with a smaller one.

Does z index work with position relative?

Note: z-index only works on positioned elements (position: absolute, position: relative, position: fixed, or position: sticky) and flex items (elements that are direct children of display:flex elements).

Why use z index in CSS?

When elements are positioned, they can overlap other elements. The z-index property specifies the stack order of an element (which element should be placed in front of, or behind, the others).


1 Answers

If you are using a modern browser that supports css3, try adding this line to the css rule for the transparent png: pointer-events: none;
It basically tells the browser to ignore all mouse events on this element.

For example:

img
{
    pointer-events: none;
}

https://developer.mozilla.org/en/css/pointer-events

Alternatively if your targeted browser does not support css3, you can capture the mouse event and then fire a new one on the underlying element.

for example if your image id is #img and your underlying element id is #elem you may do this:

$("#elem").hover(function(e){
     $("#img").mouseenter(e);
});

You might have to mess with this a little depending on how your DOMs are set up, here's the documentation http://api.jquery.com/mouseenter/

like image 98
Razor Storm Avatar answered Sep 30 '22 15:09

Razor Storm