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

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

Consider the following Web page:

 
   
     
4条回答
  •  难免孤独
    2020-12-28 13:24

    *...crickets chirping... *

    OK, I guess this question isn't going to get any stackoverflow love, so I might as well answer my own question as to the best solution I've found so far, in case another user stumbles across this question:

    The best solution I've come up with is to capture "onmousedown" and "onkeydown" for the BODY element: These events bubble, and so if a user tries to move a scrollbar on the page these global functions will fire as a by-product. Then, in these functions, simply look up event.target and attach a temporary "onscroll" event to those objects until the mouse/key is "up" again. Using that method, you can avoid "handler bloat" and still globally capture all "onscroll" events. (I think this will work for "mouse wheel" scrolling as well, but my research on that final wrinkle is still pending.)

提交回复
热议问题