简体   繁体   English

在单击和拖动任何可滚动容器的滚动条时触发全局事件

[英]Triggering a global event on click and drag of scrollbar of any scrollable container

Let me explain the problem clearly, I have many containers ( being added dynamically ) in which I have custom dropdowns. 让我清楚地解释一下这个问题,我有很多容器( 动态添加 ),我有自定义下拉列表。 The containers are set overflow: auto . 容器设置overflow: auto

So, the custom dropdown suggestions are being hidden inside that scrolling containers. 因此,自定义下拉列表建议隐藏在滚动容器中。 To fix this, I am using position: fixed to the custom dropdowns. 要解决这个问题,我使用的是position: fixed到自定义下拉菜单。

Now, I am able to see the suggestions and I am using jquery to stitch those suggestions to their respective dropdowns. 现在,我能够看到这些建议,并且我正在使用jquery将这些建议与各自的下拉菜单相结合。 But now, when I scroll a specific container, the custom dropdowns suggestions are moving away from that dropdown. 但是现在,当我滚动一个特定的容器时,自定义下拉菜单建议正在远离该下拉菜单。

So, ofcourse I need to call those stitch function again on scroll of those containers. 因此,当然我需要在滚动这些容器时再次调用这些针脚功能。 Well, I am doing them now on mousewheel and DOMMouseScroll events on Window element which is triggering whenever I scroll a container. 好了,我现在在做他们mousewheelDOMMouseScroll每当我滚动的容器中,触发窗口元素上的事件。

But now when I click on the scrollbar to move it, the event is not triggering. 但现在当我点击滚动条移动它时,事件不会触发。 Hence, I need an event which triggers when I click on the any containers scrollbar to drag it. 因此,当我点击任何容器滚动条拖动它时,我需要一个触发的事件。

window.addEventListener('DOMMouseScroll', function () {
    console.log("hello");
});
window.addEventListener('mousewheel', function () {
    console.log("hello");
});

DEMO DEMO

PS: I don't want to register events specifically to each container. PS:我不想专门为每个容器注册事件。 I want to trigger those events globally. 我想在全球范围内触发这些事件。 ( as I did in the above code by registering them to window element ). 正如我在上面的代码中所做的那样,将它们注册到window元素 )。

You need to add the capture flag on the event listener for it to work on the 'child' elements. 您需要在事件侦听器上添加捕获标志,以便它可以处理“子”元素。 By default this flag is false (docs here: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener ) 默认情况下,此标志为false (此处提供文档: https//developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

If true, useCapture indicates that the user wishes to initiate capture. 如果为true,则useCapture指示用户希望启动捕获。 After initiating capture, all events of the specified type will be dispatched to the registered listener before being dispatched to any EventTarget beneath it in the DOM tree. 启动捕获后,指定类型的所有事件将被分派到已注册的侦听器,然后再分派到DOM树中它下面的任何EventTarget。 Events which are bubbling upward through the tree will not trigger a listener designated to use capture. 向上冒泡树的事件不会触发指定使用捕获的侦听器。 See DOM Level 3 Events and JavaScript Event order for a detailed explanation. 有关详细说明,请参阅DOM Level 3事件和JavaScript事件顺序。 If not specified, useCapture defaults to false. 如果未指定,则useCapture默认为false。

window.addEventListener('scroll', function () {
    console.log("hello");
}, true); // <-- the last argument is the capture argument. 

here is your demo altered: http://jsfiddle.net/rlemon/dq5h4kor/8/ 这是你的演示改变: http//jsfiddle.net/rlemon/dq5h4kor/8/

Have you considered using jQuery 'scroll' event? 你考虑过使用jQuery'scroll'事件吗?

If you do 如果你这样做

$('.parent').scroll(function() {
   //yourcode
});

Triggering on every div element : 触发每个div元素:

   $('*').scroll(function() {
       //yourcode
    });

The above will trigger the scroll event every time you scroll within every element. 每次在每个元素中滚动时,上面都会触发滚动事件。

It will trigger both on mousewheel scroll as well as when dragging the scrollbar. 它将在鼠标滚轮滚动以及拖动滚动条时触发。 JS fiddle JS小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM