Disable scrolling in all mobile devices
Asked Answered
R

15

82

This sounds like there should be a solution for it all over the internet, but I am not sure why I cannot find it. I want to disable Horizontal scrolling on mobile devices. Basically trying to achieve this:

body{
   overflow-x:hidden  // disable horizontal scrolling.
}

This may be relevant information: I also have this in my head tag, because I also do not wish the user to be able to zoom:

<meta content='width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;' name='viewport' />
<meta name="viewport" content="width=device-width" />

Thanks

Rumph answered 14/5, 2012 at 23:39 Comment(2)
refer this to disable scrolling : #2528399Gable
That link is more for app developers using android webviewAmiraamis
L
137
html, body {
  overflow-x: hidden;
}
body {
  position: relative;
}

The position relative is important, and i just stumbled about it. Could not make it work without it.

Lewak answered 24/6, 2014 at 16:54 Comment(5)
Agree with @daekano This answer Should be marked as the accepted answer. This worked for me also.Silicone
Could anyone explain how the position:relative helps this work?Strander
I can't believe something as small as position: relative; was keeping it from working. Thanks!Pegues
It works but screws up the smooth scrolling that touch devices have... (I can't seem to push up and see the page scroll, it stops scrolling as soon as I release my finger)Counterreply
For whatever reason this works on android devices but not on any IOS devices I tryLeonleona
E
33

cgvector answer didn't work for me, but this did:

document.body.addEventListener('touchstart', function(e){ e.preventDefault(); });

I wouldn't leave it just like that, a smarter logic is needed to select when to prevent the scrolling, but this is a good start.

Taken from here: Disable scrolling in an iPhone web application?

Eductive answered 24/5, 2012 at 14:3 Comment(7)
you understand, that you disable even vertical page scrolling with that, right?Unquiet
Yeah, that's why it says that a smarter logic is needed to select when to prevent the scrolling.Eductive
I cannot use this, because this also prevents any HTML 5 contextmenu to be shown (supported by Firefox 20 for Android).Graph
is it just me or does this seem totally reckless?Toritorie
Chango - perfect solution! As I use custom scroll script on my mobile web-app, this piece of code finally solved all my issues at once!Noto
Worked in iOS with Safari, but broke Chrome on Android. Tobl's answer worked perfectly for me.Walkabout
This also stops anything being scrolled inside the body. Even scrollable divs!Mimosa
S
24

For future generations:

To prevent scrolling but keep the contextmenu, try

document.body.addEventListener('touchmove', function(e){ e.preventDefault(); });

It still prevents way more than some might like, but for most browsers the only default behaviour prevented should be scrolling.

For a more sophisticated solution that allows for scrollable elements within the nonscrollable body and prevents rubberband, have a look at my answer over here:

https://mcmap.net/q/57358/-how-to-disable-rubber-band-in-ios-web-apps

Superfetation answered 26/11, 2013 at 12:45 Comment(2)
Just tested this and worked perfectly with Safari on iOS, and both Chrome and Firefox on Android.Walkabout
Current Chrome versions need this: document.body.addEventListener('touchmove', function(e){ e.preventDefault(); }, { passive: false });. Note that the objects becomes true thus event capturing is used.Latashialatch
L
19

I suspect most everyone really wants to disable zoom/scroll in order to put together a more app-like experience; because the answers seem to contain elements of solutions for both zooming and scrolling, but nobody's really nailed either one down.

Scrolling

To answer OP, the only thing you seem to need to do to disable scrolling is intercept the window's scroll and touchmove events and call preventDefault and stopPropagation on the events they generate; like so

window.addEventListener("scroll", preventMotion, false);
window.addEventListener("touchmove", preventMotion, false);

function preventMotion(event)
{
    window.scrollTo(0, 0);
    event.preventDefault();
    event.stopPropagation();
}

And in your stylesheet, make sure your body and html tags include the following:

html:
{
    overflow: hidden;
}

body
{
    overflow: hidden;
    position: relative;
    margin: 0;
    padding: 0;
}

Zooming

However, scrolling is one thing, but you probably want to disable zoom as well. Which you do with the meta tag in your markup:

<meta name="viewport" content="user-scalable=no" />

All of these put together give you an app-like experience, probably a best fit for canvas.

(Be wary of the advice of some to add attributes like initial-scale and width to the meta tag if you're using a canvas, because canvasses scale their contents, unlike block elements, and you'll wind up with an ugly canvas, more often than not).

Lyric answered 3/2, 2016 at 7:43 Comment(4)
After searching and trying for hours, only this approach worked for me. I can't tell how much I thank you mate, you saved my life!Bankrupt
Setting overflow for html is evidently necessary. Without it but with all the rest of the advices from this thread there was still some scrolling happening. Preventing events was not needed in my case.Any
as mentioned by @brannigan in another comment, use window.addEventListener("touchmove", preventMotion, { passive: false });Barbitone
Confirmed working on Android just now, but only with the above modification: window.addEventListener("touchmove", preventMotion, { passive: false });Rack
G
14

Try adding

html {
  overflow-x: hidden;
}

as well as

body {
  overflow-x: hidden;
}
Gravitate answered 21/10, 2013 at 14:6 Comment(2)
Read my answer. I said add it to the HTML tag, not just body.Gravitate
on some mobile devices the scrolling gets disabled completely.Alexipharmic
F
10

In page header, add

<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no">

In page stylesheet, add

html, body {
  overflow-x: hidden;
  overflow-y: hidden;
}

It is both html and body!

Forestall answered 5/10, 2015 at 19:15 Comment(0)
S
8

use this in style

body
{
overflow:hidden;
width:100%;
}

Use this in head tag

<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0" />
<meta name="apple-mobile-web-app-capable" content="yes" />
Studied answered 15/5, 2012 at 6:25 Comment(1)
overflow:hidden and width:100% both in body and html were sufficient. thks! (in iPhone!)Chiao
W
4

The CSS property touch-action may get you what you are looking for, though it may not work in all your target browsers.

html, body {
    width: 100%; height: 100%;
    overflow: hidden;
    touch-action: none;
}
Wendiwendie answered 13/6, 2019 at 18:20 Comment(0)
H
3

After having no success trying all the answers I managed to turn my mobile scroll off by simply adding:

html,
body {
  overflow-x: hidden;
  height: 100%;
}

body {
  position: relative;
}

Its important to use % not vh for this. The height: 100% was something I had been missing all along, crazy.

Holds answered 8/8, 2020 at 12:22 Comment(1)
I wish I had stumbled upon your answer 24 hours earlier as it would have saved me a whole bunch of time only to arrive at the same conclusion. The height: 100% was the missing part of what I needed to make it all work. Because every child of body I used was position: absolute, it was giving the body element no set height.Contrast
K
3

Setting position to relative does not work for me. It only works if I set the position of body to fixed:

document.body.style.position = "fixed";
document.body.style.overflowY = "hidden";
document.body.addEventListener('touchstart', function(e){ e.preventDefault()});
Knight answered 14/11, 2020 at 1:2 Comment(0)
H
2

This works for me:

window.addEventListener("touchstart", function(event){
             if(event.target.tagName=="HTML" || event.target.tagName=="BODY"){
                     event.preventDefault();
             }
} ,false);

It does not work 100% and I also added this:

window.addEventListener("scroll",function(){
    window.scrollTo(0,0)
},false)
Humanitarian answered 29/10, 2013 at 22:40 Comment(0)
T
1

For Apple devices position: relative does not work. The following code works on all devices:

html, body {
  overflow: hidden;
  position: fixed;
}
Trapezoid answered 2/6, 2021 at 5:8 Comment(0)
C
1

I'm late to the party, but I'm adding this answer because none of the other things I tried worked in my specific situation. No combination of capturing touchmove, scroll etc events had any effect, and position: relative on body made everything disappear.

I found that I had to add height: 100% on the HTML and BODY elements. It's possible that not all of the following rules are required, but it took me long enough to stumble upon this magic combination and it appears to work.

html {
    height: 100%;
    overflow: hidden;
}
body{
    margin: 0;
    overflow: hidden;
    height: 100%;
    position: relative;
}

In addition to this I also had the following in the HTML head:

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

My app made heavy use of absolutely position elements, some of which were partially off-screen at different times as they slid out of view. As a result, the body element had no content in it to give the body any area/size.

I found that the "scrolling" that was happening on mobile devices was a result of the browser not knowing the extent of the page's size in order to do its viewport (scaling to device-width) calculation. It seems that the body having 0px height was failing to convey to the browser either the height or width of the page, so setting the height of body to 100% was key in the solution.

Edit: looking at the answers again after finding my solution, I realise the other answer by "Toms Codery" is essentially the same solution as mine, albeit his is only in the x-direction.

Contrast answered 29/7, 2021 at 3:9 Comment(0)
H
0

The simplest way which is pretty much straight forward with only CSS Codes:

body, html {
    overflow-x: hidden;
    position: relative;
}
Hierodule answered 22/2, 2021 at 22:8 Comment(0)
B
-2

The following works for me, although I did not test every single device there is to test :-)

$('body, html').css('overflow-y', 'hidden');
$('html, body').animate({
    scrollTop:0
}, 0);
Burgonet answered 31/8, 2015 at 13:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.