最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript - Cancel Scroll event - Stack Overflow

programmeradmin6浏览0评论

I want to disable the scroll in one of my pages. I don't want to write

scroll(0,0) or 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?

I want to disable the scroll in one of my pages. I don't want to write

scroll(0,0) or 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?

Share Improve this question edited Jun 21, 2011 at 16:03 Bill the Lizard 406k211 gold badges572 silver badges889 bronze badges asked Aug 4, 2010 at 11:33 NLVNLV 21.6k41 gold badges120 silver badges186 bronze badges 2
  • See stackoverflow./questions/1459676/… – Crescent Fresh Commented Aug 4, 2010 at 11:38
  • stackoverflow./questions/4770025/… although that is to temporarily disable scrolling, you can set it to permanently disable it. You should use that instead of using overflow: hidden because Chrome has a bug that you can still scroll on overflow: hidden pages using the mousewheel, arrow keys, pgup and pgdn keys, and touch scrolling if you're on a touch device. The accepted answer in that link provides a way that effectively disables it. – markasoftware Commented Jul 22, 2013 at 17:36
Add a ment  | 

4 Answers 4

Reset to default 6
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.

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

Create a div that 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.

发布评论

评论列表(0)

  1. 暂无评论