Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: How to prevent scrolling on iOS Safari? [duplicate]

Tags:

I created a modal. When the modal is open I'm stopping the scroll on Desktop with overflow:hidden; to the <body>. It's working.

But it's not working on my iPhone 6s Mobile Safari.

How can I prevent scrolling when the modal is open in mobile safari?

like image 650
Henry Zhu Avatar asked Mar 01 '15 06:03

Henry Zhu


People also ask

How do I turn off scrolling on iPhone CSS?

The most straightforward way for disabling scrolling on websites is to add overflow: hidden on the <body> tag. Depending on the situation, this might do the job well enough. If you don't have to support older versions of iOS, you can stop reading here. // src/utils/scroll-lock.

How do I fix my scroll on safari?

Try double tapping on either side of the screen at edges. Use a two finger press inside the window that you're trying to scroll and move them up or down simultaneously. Or pinch your thumb and forefinger together and move it up or down.

How do you stop scrolling in CSS?

Disabling scroll with only CSS. There's another way to disable scrolling that is commonly used when opening modals or scrollable floating elements. And it is simply by adding the CSS property overflow: hidden; on the element you want to prevent the scroll.


2 Answers

Combine it with position: fixed to do this on iOS…

overflow: hidden; position: fixed; 
like image 98
matharden Avatar answered Sep 29 '22 02:09

matharden


There isn't (to my knowledge) a great way to accomplish this with CSS without repercussions on the UX.

While it's Javascript, and not CSS, the best way I've found to do this is the following:

// Disable scrolling. document.ontouchmove = function (e) {   e.preventDefault(); }  // Enable scrolling. document.ontouchmove = function (e) {   return true; } 
like image 36
AsLittleDesign Avatar answered Sep 29 '22 01:09

AsLittleDesign