How to capture all scrolling events on a page without attaching an onscroll handler to every single container

后端 未结 4 790
青春惊慌失措
青春惊慌失措 2020-12-28 12:57

Consider the following Web page:

 
   
     
4条回答
  •  长情又很酷
    2020-12-28 13:26

    The following works fine when you want to i.e. close a dialog after anything in the background is scrolled:

    var scrollListener = function(e) {
        // TODO: hide dialog
        document.removeEventListener('scroll', scrollListener, true);
    };
    
    document.addEventListener('scroll', scrollListener, true);
    

提交回复
热议问题