[英]How to stop scrolling
How can I prevent the page from sliding? 如何防止页面滑动? Let say:
说:
let timeout = 0; document.onmousewheel = () => { window.clearTimeout(timeout); timeout = setTimeout(() => { console.log("Stop scrolling"); }, 20); } document.onscroll = () => { console.log("scrolling"); }
p { height: 300px; }
<p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p>
The idea here is that while I am not rolling the wheel of the mouse, it should not be scrolling. 这里的想法是,当我不滚动鼠标滚轮时,它不应滚动。 Sometimes if you do it too fast it continues scrolling for a while.
有时,如果您做得太快,它将继续滚动一段时间。
How can I prevent this? 我该如何预防?
Using -webkit-overflow-scrolling
: 使用
-webkit-overflow-scrolling
:
let timeout = 0; document.onmousewheel = () => { window.clearTimeout(timeout); timeout = setTimeout(() => { console.log("Stop scrolling"); }, 20); } document.onscroll = () => { console.log("scrolling"); }
p { height: 300px; } body { -webkit-overflow-scrolling: auto; }
<p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p>
It is not working as I expected, maybe I'm using it wrong? 它没有按我预期的那样工作,也许我用错了吗?
You can do it with css 你可以用CSS做到这一点
https://developer.mozilla.org/en-US/docs/Web/CSS/-webkit-overflow-scrolling https://developer.mozilla.org/zh-CN/docs/Web/CSS/-webkit-overflow-scrolling
Just add property -webkit-overflow-scrolling: auto
to your body
. 只需将属性
-webkit-overflow-scrolling: auto
到您的body
。
I can't really test this, but does return false on the scroll event prevent the scroll? 我不能真正测试一下,但是在滚动事件上返回false会阻止滚动吗?
var timeout = 0, doScroll; document.onmousewheel = function() { doScroll = 1; window.clearTimeout(timeout); timeout = setTimeout( function() { console.log("Stop scrolling"); doScroll = 0; }, 20); } document.onscroll = function() { if (doScroll == 0) return false; console.log("scrolling"); }
p { height: 300px; }
<p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p><p> Sample </p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.