Javascript:有没有办法检测用户滚动,但不通过代码完成滚动(即scrollLeft等)?
所以。我正在制作一个带有按钮的页面;单击按钮可以平滑地将页面(实际上是一个容器)滚动到位于更右侧的锚点。这是通过 container.scrollLeft
完成的。
现在我试图做到这一点,以便当用户手动滚动(滚动条、鼠标滚轮、箭头键等)时,自动平滑滚动立即停止。
我尝试使用 container.addEventListener('scroll',StopScroll,false);
执行此操作,但这会触发任何滚动,甚至通过代码完成,而不仅仅是由用户完成。 有没有办法只检测用户滚动?或者也许是一个解决方法?
另外,我不想使用 Jquery,但如果这是唯一的方法,我会改用它。
So. I'm making a page with buttons; clicking the buttons smoothly scrolls the page (actually a container) to anchors located further right. This is done with container.scrollLeft
.
Now I'm trying to make it so that when the user scrolls manually (scrollbar, mousewheel, arrow keys etc), the automated smooth scrolling instantly stops.
I've tried doing this with container.addEventListener('scroll',StopScroll,false);
but this fires up for any scroll, even done through code, not just done by the user.
Is there a way to detect only user scrolling ? Or maybe a work-around ?
Also, I'd rather not use Jquery, but I'll switch to it if it's the only way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有一种感觉,DOM 滚动和事件滚动的管理方式相同,因此无法区分。
但是,您可能会挂钩 mousedown/keydown 并在按住键或鼠标按钮时更新变量,并且仅在设置了该变量时才执行scrollStop?
I've got a feeling that DOM scrolling and event scrolling are managed the same way, and therefore indistinguishable.
However, you might hook into mousedown/keydown and update a variable when a key or mouse button is held, and only perform scrollStop if that variable is set?