-1

I broke my head trying to disable the following javascript for mobile devices. I'm a dummy in javascript so would appreciate any help.

$(window).scroll(function() {
    if($(this).scrollTop() > 50)  /*height in pixels when the navbar becomes non opaque*/ 
    {
        $('.navbar-sticky').addClass('sticky');
    } else {
        $('.navbar-sticky').removeClass('sticky');
    }
});
Morgari
  • 467
  • 2
  • 7
  • 21
  • [How do I remove scripts for responsive mobile](https://stackoverflow.com/questions/11335684/how-do-i-remove-scripts-for-responsive-mobile) – Sudhir Ojha Apr 17 '19 at 07:48
  • Possible duplicate of [How do I remove scripts for responsive mobile](https://stackoverflow.com/questions/11335684/how-do-i-remove-scripts-for-responsive-mobile) – Chris G Apr 17 '19 at 07:50

4 Answers4

0

You can add given below code on starting point of javascript and return if it is any mobile device

if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|Windows Phone/i.test(navigator.userAgent)) {
        return 
}

$(window).scroll(function() {
    if($(this).scrollTop() > 50)  /*height in pixels when the navbar becomes non opaque*/ 
    {
        $('.navbar-sticky').addClass('sticky');
    } else {
        $('.navbar-sticky').removeClass('sticky');
    }
});
Chandan Kumar
  • 648
  • 4
  • 18
0

Check $(window).width:

$(() => {
    if ($(window).width > 481) {
        $(window).scroll(function() {
            if($(this).scrollTop() > 50) {
                $('.navbar-sticky').addClass('sticky');
            } else {
                $('.navbar-sticky').removeClass('sticky'); 
            }
        });
    }
});
Jack Bashford
  • 38,499
  • 10
  • 36
  • 67
0

You can check if you are loading website on mobile and add conditon before you run you code i don't have resources to test this out but hope this helps you

        var isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
        var element = document.getElementById('text');
        if (isMobile) {
            //dont use 
        } else {
            $(window).scroll(function() {
                   if($(this).scrollTop() > 50)  /*height in pixels when the navbar becomes non 
                 opaque*/ 
                {
                $('.navbar-sticky').addClass('sticky');
                } else {
                 $('.navbar-sticky').removeClass('sticky');
               }
            });
        }
0

You can try this simple CSS.

@media screen and (max-width:479px){
#mobile-header {
  position: Static !important;
}
Khushbu Vaghela
  • 735
  • 2
  • 5
  • 16