Listening to all scroll events on a page

后端 未结 4 951
借酒劲吻你
借酒劲吻你 2021-02-07 02:39

Background:

I\'m writing a component that opens up a sub-menu on click. I can\'t know where this component will be placed on the page or how far it will

4条回答
  •  失恋的感觉
    2021-02-07 02:59

    You should be able to attach a document-level listener with a third parameter of true to capture the scroll events on all elements. Here's what that looks like:

    document.addEventListener('scroll', function(e){ }, true);
    

    The true at the end is the important part, it tells the browser to capture the event on dispatch, even if that event does not normally bubble, like change, focus, and scroll.

    Here's an example: http://jsbin.com/sayejefobe/1/edit?html,js,console,output

提交回复
热议问题