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. B
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" />
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.
In page header, add
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-sacle=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!
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.
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;
}
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).
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);
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.