简体   繁体   中英

disable scroll only on mobile Javascript

I need to disable scrolling for a specific DIV only for mobile devices

if mobile scrolling disabled else scrolling on

$(function() {
    var offset = $('#sidebar-wrapperleft').offset();
    var topPadding = 0;

    $(window).scroll(function() { 
       if ($(window).scrollTop() > offset.top) { 
           $('#sidebar-wrapperleft').stop().animate({
               marginTop: $(window).scrollTop() - offset.top + topPadding
           });
       } else{
           $('#sidebar-wrapperleft').stop().animate({
               marginTop: 0
           });
       };
   });
});

You need to be aware that "mobile detection" is something that can cause you some headache. If possible, you should rely on screen dimensions to adapt your content.

Mobile detection is problematic because, first of all, what should be considered mobile? Is an iPad mobile or not? As you can see, there are some gray areas in this concept.

There is another thread here ( What is the best way to detect a mobile device in jQuery? ) where you can find some algorithms based on Browser Agent, and they probably need constant improvement as soon new devices come to market.

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