jQuery hover div underneath another layer

非 Y 不嫁゛ 提交于 2019-12-04 23:56:45

If you can use CSS3 then you can set pointer-events:none for the absolutely positioned elements, see demo here.

All modern browsers support this property - only IE9 and below and Opera Mini do not support it (at the time of writing). It also means you will not have any pointer-events for those elements which might not be exactly what you want.

The way I see it, you have a few options:

  • Do as Ivarska recommended, and create an empty element over all of them and use that for the trigger.
  • Bind to the mousemove on the entire page and find when it's "inside the box" (i.e. over the target element)--basically, re-invent the hover event. (But not this adds some pretty serious overhead to your page)
  • Redesign

You may be also able to bind to just the target (i.e. A) and any elements you have that may overlap A, then just check if the mouse position within the other control would hypothetically also be inside the A control. Less overhead than binding to the page and checking, but still more than typical.

Use e.relatedTarget to determine if the user hovered over one of you nested (absolute) elements. Maybe easier if you give your map labels a class

e.g: To show you labels during a hover state on the map, and only hide them when the user leaves the map

    $("#map").hover(
      function(e) { 
        $(this).find(".labels").fadeIn();
      },

      function(e) {
        if( $(e.relatedTarget).hasClass("maplabel") ) {
          //The user has hovered over a label...Do nothing (or some other function)
        } else {
          // User has left the map and isn't over a label
          $(this).find(".labels").fadeOut();
      }
    );

The only solution I've got in mind right now is to create an invisible element at the top and give it the hover trigger.

You could make the z-index of the A element higher then that of the B, C and D elements.

Although there are different ways to fix this, probably the simplest would be to add the hover event to all of the elements:

HTML

<div id="a" class="hover"></div>
<div id="b" class="hover"></div>
<div id="c" class="hover"></div>
<div id="d" class="hover"></div>
<div id="state">unhovered</div>

CSS

#a {
    width: 350px;
    height: 300px;
    border: 1px solid #000;
    background-color: #ccc;
}
#b {
    position: absolute;
    top: 35px;
    left: 35px;
    width: 35px;
    height: 30px;
    border: 1px solid #000;
    background-color: #cca;
}
#c {
    position: absolute;
    top: 85px;
    left: 85px;
    width: 35px;
    height: 30px;
    border: 1px solid #000;
    background-color: #cca;
}
#d {
    position: absolute;
    top: 85px;
    left: 135px;
    width: 35px;
    height: 30px;
    border: 1px solid #000;
    background-color: #cca;
}

jQuery

$(document).ready(function(){
    $('.hover').hover(
        function(){
            $('#state').text('hovered');
        },
        function(){
            $('#state').text('unhovered');
        }
    );
});

Example here: http://jsfiddle.net/userdude/H5TAG/

The z-index adjustment someone else mentioned works. Make the z-index higher for the item you want to be recognized by hover.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!