2017-06-19 43 views
1

当光标位于具有较高z-index的另一个元素上时,有什么办法可以在容器上启用鼠标滚轮吗?这里是例子:https://jsfiddle.net/dkdghj7r/z-index较高的元素滑轮

$('div').on('mousewheel', function(event) { 
 
    event.preventDefault(); 
 
    console.log('Scroll!') 
 
})
div { 
 
    background: yellow; 
 
    height: 300px; 
 
    width: 500px; 
 
} 
 

 
span { 
 
    background-color: red; 
 
    display: block; 
 
    height: 100px; 
 
    left: 200px; 
 
    position: fixed; 
 
    top: 100px; 
 
    width: 100px; 
 
    z-index: 10; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div></div> 
 

 
<span></span>

+0

只有将具有较高'z-index'的元素放置在具有较低元素的元素内。 – user13286

回答

0

如何计算由框中的网页上所占的面积,让鼠标的页面X/Y,并检查是否鼠标框的区域内:

$(document).on('wheel', function() { 
    var offset = $('div').offset(); 
    var width = $('div').width(); 
    var height = $('div').height(); 

    if (lastEvent.pageX > offset.left && lastEvent.pageX < offset.left + width 
     && lastEvent.pageY > offset.top && lastEvent.pageY < offset.top + height) { 
    console.log('Scolled inside yellow box!'); 
    } 
}); 

var lastEvent = { 
    pageX: -1, 
    pageY: -1 
}; 

$(document).on('mousemove', function(event) { 
    lastEvent = { 
    pageX: event.pageX, 
    pageY: event.pageY 
    }; 
}); 

https://jsfiddle.net/37d9z411/

+1

只是一个说明,修改你的小提琴使用'wheel'事件而不是'mousewheel'。 'wheel'是标准事件:https://developer.mozilla.org/en-US/docs/Web/Events/mousewheel – PaulBGD

+0

可惜这个解决方案太复杂了。但是,谢谢,我不知道'wheel'事件,所以我必须为Firefox使用'mousewheel'和'DOMMouseScroll'。 :-) – Vesmy