[英]How to move parent scroll bar with mouse wheel when mouse is over a child scrollable HTML element?
I have the following issue. 我有以下问题。 I have parent div which contains several child divs.
我有父div,其中包含几个子div。 Both parent and children have scroll bars.
父母和孩子都有滚动条。 I want to move only the scroll bar of the parent when my mouse is over that element, no matter the mouse hover a scrollable child div or don't.
当鼠标悬停在该元素上时,无论鼠标悬停在可滚动的子div上,还是不移动,我都只想移动父控件的滚动条。 In other words I want to prevent event handling on the child event and propagate event to the parent.
换句话说,我想防止对子事件进行事件处理,并将事件传播给父事件。 Something like this:
像这样:
<div id='parent' style='overflow-y:auto;height:200px;width:100%'>
<div class='child' style='overflow-y:auto;height:100px'>
</div>
<div class='child' style='overflow-y:auto;height:100px'>
</div>
<div class='child' style='overflow-y:auto;height:100px'>
</div>
</div>
<script>
$('.child').bind('mousewheel DOMMouseScroll', function(e) {
$(e.target).parent().trigger(e);
});
</script>
Note: jQuery is allowed. 注意:允许使用jQuery。
This is the right solution thanks to @DavidFregoli: 感谢@DavidFregoli,这是正确的解决方案:
$('.child').on('mousewheel DOMMouseScroll', function(e) {
var $div = $(this),
$parent = $div.parent(),
scrollAmount = 30,
currentScroll = $parent.scrollTop(),
newScroll = currentScroll + (e.originalEvent.wheelDelta < 0 ? scrollAmount : -scrollAmount);
$parent.scrollTop(newScroll);
return false;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.