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 to execute an event before a div with overflow: scroll scroll. The problem with using the .scroll event is that this occurs after scrolling through the div, and not earlier.

+7
javascript jquery
source share
2 answers

No, there is no such event. The scroll event cannot be canceled (for obvious reasons), and I would suggest that it fires after the action, so that the scrollTop and scrollLeft properties are accurate on access.

Possible workarounds may be to capture mousewheel / DOMMouseScroll events and key change events for pages up, down, down, up and down. However, there is no 100% method - you can never stop user interaction with browser scroll components. The only true solution is to collapse your own scrollbars.

If you just want to find the amount of user scroll, you can set a timer to store the current scrollLeft / scrollTop in a variable, and then check them and the new values ​​in the scroll event.

+6
source share

I solved it in a rough way, which turned out to be surprisingly unstable:

I update the position myself after checking that the position is within the range that I want, using position = fixed and changing the upper value.

0
source share

All Articles