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

javascript - Prevent scrolling a div with CSS? - Stack Overflow

programmeradmin2浏览0评论

I'm looking at this demo page:

.html

Which has this code behind it:

In general it works great but when the menu is open on my macbook I can use two fingers to scroll and move the white body div around. How could I prevent that using CSS?

I'm looking at this demo page:

http://www.ymc.ch/sandbox/hamburger/mobile-menu-demo.html

Which has this code behind it:

https://github.com/ymc-thzi/mobile-menu-hamburger

In general it works great but when the menu is open on my macbook I can use two fingers to scroll and move the white body div around. How could I prevent that using CSS?

Share Improve this question asked Apr 12, 2014 at 3:04 Aron SolbergAron Solberg 6,8589 gold badges34 silver badges48 bronze badges 3
  • I suspect you'll need JS for that. BTW, have you tried to play with developer.mozilla.org/en-US/docs/Web/CSS/pointer-events – Roko C. Buljan Commented Apr 12, 2014 at 3:11
  • @roko js is not needed, see my simple answer – Kevin Florida Commented Apr 12, 2014 at 3:13
  • The usual overflow in CSS doesn't work. I'm not even sure if this counts as "scrolling" per say but when I use two fingers on my track pad I can move the white content div around the screen (when the menu is open). Do other people see this issue as well? – Aron Solberg Commented Apr 12, 2014 at 4:48
Add a comment  | 

2 Answers 2

Reset to default 14

If you always want to prevent scrolling, you could use something like:

div {
  overflow-x: hidden;
  overflow-y: hidden;
}

Just be aware that any content that extends past the edge of the div will be hidden :)

Otherwise, if you are just looking for a web mobile menu, check out jQuery mobile.

The scroll bars in the example URL are in the body, so you need to set the overflow property on the body to hidden when the menu is open and to auto when the menu is closed.

In the JavaScript file, you can add jQuery(document.body).css('overflow', 'hidden') in jQuery("#hamburger").click function and jQuery(document.body).css('overflow', 'auto') in the jQuery("#contentLayer").click function.

发布评论

评论列表(0)

  1. 暂无评论