-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
  • 524
  • 2
  • 8
  • 24
  • [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) –  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
  • 799
  • 7
  • 23
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
  • 43,180
  • 11
  • 50
  • 79
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');
               }
            });
        }
Faizan Ul Haq
  • 454
  • 2
  • 7
  • 23
0

You can try this simple CSS.

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