简体   繁体   中英

How to disable scroll on mobile browser without checking device?

Interesting question:

How to disable scroll on mobile device BUT in code no need to check mobile browser o etc.

I'm writing mobile version of web site. In web browser my function works well!

When i calling Scroll("off); it's stops. But on mobile device (ios) i still can touchmoving content.

Can someone help to modify function for this case? Result must be: No scroll on web, NO touching moves on mobile device.

Here what i have:

function Scroll(str)
    {
      var scrollPosition = [
        self.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft,
        self.pageYOffset || document.documentElement.scrollTop  || document.body.scrollTop
      ];

      if(str=="off")
      {
        var html = jQuery("html");
        html.data("scroll-position", scrollPosition);
        html.data("previous-overflow", html.css("overflow"));
        html.css("overflow", "hidden");
        window.scrollTo(scrollPosition[0], scrollPosition[1]);
      }
      else
      {
        var html = jQuery("html");
        var scrollPosition = html.data("scroll-position");
        html.css("overflow", html.data("previous-overflow"));
        window.scrollTo(scrollPosition[0], scrollPosition[1]);
      }
    }

Solution is:

$('body').bind('touchmove', function(e){e.preventDefault()}); 
$("body").unbind("touchmove");  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM