简体   繁体   English

JavaScript - 取消滚动事件

[英]JavaScript - Cancel Scroll event

I want to disable the scroll in one of my pages. 我想在我的一个页面中禁用滚动。 I don't want to write 我不想写

scroll(0,0) or scrollTo(0,0) scroll(0,0)scrollTo(0,0)

in the scroll event which will give a 'jumpy' nature. 在滚动事件中,这将产生“跳跃”的性质。 Can't I have something like 我不能有类似的东西

event.preventDefault()

for canceling the event? 取消活动?

document.body.style.overflow = 'hidden';

The only acceptable way of preventing scrolling is making the content fit inside the window. 防止滚动的唯一可接受的方法是使内容适合窗口内部。 Anything else, and you're pissing users off. 还有其他的东西,你在惹恼用户。

You can also use position: fixed CSS - but still, if it's larger than the window, the rest is unusable, so you might as well follow the suggestion #1. 你也可以使用position: fixed CSS - 但是,如果它比窗口大,那么剩下的就不可用了,所以你不妨遵循#1的建议。

I wonder if overflow: hidden; 我想知道是否overflow: hidden; will work by putting it on the html, body selector in CSS? 将它放在CSS中的html, body选择器上吗?

Create a div that fills the whole area and set that to overflow: hidden . 创建一个填充整个区域的div并将其设置为overflow: hidden That way, you will never get scroll bars and no scroll events will be created. 这样,您将永远不会获得滚动条,也不会创建滚动事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM