How to make a jquery code run only on the desktop version of the site

-4

I have the following problem in the code below:

// Muda o menu de imagem pra texto
    var x = $(window).width();

        if(x >= 590){
            $(document).on("scroll",function(){

                    if($(document).scrollTop()>100){ //QUANDO O SCROLL PASSAR DOS 100px DO TOPO
                        $("#nav_logo_img").removeClass("imgON").addClass("imgnone");
                        $("#nav_logo_texto").addClass("logo_textoON").fadeIn();
                        $("nav").addClass("nav_pequeno").addClass("bg_navbar");
                    } else{
                        $("#nav_logo_img").removeClass("imgnone").addClass("imgON");
                        $("#nav_logo_texto").removeClass("logo_textoON").fadeOut();
                        $("nav").removeClass("nav_pequeno").removeClass("bg_navbar");
                    }

            });
        }else{
            // não muda nada
        }

This code is so that the navbar in the 100px size decreases, and change the color and logo of the site, it is running 100%. The problem is that in the mobile version the text already appears by default, plus the code above causes the logo to flash on the screen, I tried to use an if to change this in the mobile version but not to achieve, without someone being able to give me a light on how to make this effect appear in the desktop version ??

    
asked by anonymous 30.12.2018 / 19:13

1 answer

2

If the question is just to limit the execution of the code on the desktop, you can use the library (it's only 4K):

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/isMobile.min.js"></script>

Theninyourjavascript,insteadofhavingifbywindowswidthyoucanputitsoon!isMobile.any

Thecodelookssomethinglikethis:

if(!isMobile.any){$(document).on("scroll",function(){

                if($(document).scrollTop()>100){ //QUANDO O SCROLL PASSAR DOS 100px DO TOPO
                    $("#nav_logo_img").removeClass("imgON").addClass("imgnone");
                    $("#nav_logo_texto").addClass("logo_textoON").fadeIn();
                    $("nav").addClass("nav_pequeno").addClass("bg_navbar");
                } else{
                    $("#nav_logo_img").removeClass("imgnone").addClass("imgON");
                    $("#nav_logo_texto").removeClass("logo_textoON").fadeOut();
                    $("nav").removeClass("nav_pequeno").removeClass("bg_navbar");
                }

        });
    }else{
        // não muda nada
    }
    
30.12.2018 / 20:30