jQuery beforeScroll事件 [英] jQuery beforeScroll event
问题描述
jQuery中是否存在beforeScroll事件?还是可以完全复制此类事件?
Is there a beforeScroll event in jQuery? Or can this type of event be replicated at all?
在一个场景中,我们需要在溢出:scroll的div滚动之前执行一个事件.使用.scroll事件的问题在于,它是在div滚动之后而不是之前滚动的.
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.
推荐答案
不,没有这样的事件.滚动事件无法取消(出于明显的原因),我推测它会在操作后触发,以便在访问时 scrollTop 和 scrollLeft 属性是准确的.
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.
可能的解决方法可能是捕获 mousewheel/DOMMouseScroll 事件以及用于向上翻页,向下翻页,向上翻页,向下翻页等键的keydown事件.但是,没有100%的方法-您将永远无法阻止用户与浏览器的滚动条组件进行交互.唯一真正的解决方案是滚动自己的滚动条.
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.
如果您只是想查找用户滚动的数量,则可以设置一个计时器以将当前 scrollLeft/scrollTop 存储在变量中,然后将它们与 scroll 事件.
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.
这篇关于jQuery beforeScroll事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!