JavaScript - 取消滚动事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3405060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:04:53  来源:igfitidea点击:

JavaScript - Cancel Scroll event

javascriptscroll

提问by NLV

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?

取消活动?

回答by Marcelo

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

回答by Amadan

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: fixedCSS - but still, if it's larger than the window, the rest is unusable, so you might as well follow the suggestion #1.

您也可以使用position: fixedCSS - 但是,如果它大于窗口,其余部分将无法使用,因此您不妨遵循建议 #1。

回答by dsamarin

I wonder if overflow: hidden;will work by putting it on the html, bodyselector in CSS?

我想知道overflow: hidden;将它放在html, bodyCSS 中的选择器上是否可行?

回答by Aaron Digulla

Create a divthat fills the whole area and set that to overflow: hidden. That way, you will never get scroll bars and no scroll events will be created.

创建一个div填充整个区域并将其设置为overflow: hidden. 这样,您将永远不会得到滚动条,也不会创建滚动事件。