Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery beforeScroll event

Is there a beforeScroll event in jQuery? Or can this type of event be replicated at all?

We have a scenario where we need perform an event before a div with overflow:scroll has been scrolled. The problem with using the .scroll event is that this is raised after the div has been scrolled rather than before.

like image 252
Robert W Avatar asked Jul 28 '10 10:07

Robert W


1 Answers

No, there's no such event. The scroll event cannot be cancelled (for obvious reasons) and I would speculate that it fires after the action so that the scrollTop and scrollLeft properties are accurate when accessed.

Possible workarounds might be to capture the mousewheel/DOMMouseScroll events and the keydown events for page up, page down, up, down, etc. keys. There's no 100% method, though - you'll never be able to stop the user from interacting with the browser's scrollbar components. The only true solution there is to roll your own scrollbars.

If you're just looking to find the amount the user scrolled, you could set a timer to store the current scrollLeft/scrollTop in a variable and then check them vs the new values in the scroll event.

like image 81
Andy E Avatar answered Oct 20 '22 12:10

Andy E