V
V
Vadim Belkin2015-09-25 08:48:43
css
Vadim Belkin, 2015-09-25 08:48:43

How to prevent body from scrolling on mobile/touch devices when overflow: hidden?

When opening a modal window with position:fixed, a class is added to the body with the parameter overflow: hidden and height: 100%. Only the content of the modal scrolls from the computer, but from the phone (tested on the iPhone and Windows Phone 8/8.1) the page itself will scroll (you can take the bootstrap modal window as an example ). How can I prevent page scrolling, leaving the ability to scroll inside the modal?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
ITZver, 2017-11-22
@ITZver

Here is my solution via CSS.

A
Alexander, 2015-09-25
@kentuck1213

Specify meta tag

<meta name="viewport" content="width=device-width, initial-scale=1">

body{
overflow:hidden;
}
MyModal#{
overflow:scroll;
}
Something like this.

G
Gregory, 2015-09-25
@grigruss

$(window).scroll(function(){
   if($('body').scrollTop()>0)
        $('body').scrollTop(0);
});

PS: It can be even simpler:
$(window).scroll(function(){
        $('body').scrollTop(0);
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question