-1

I have this code:

jQuery(document).ready(function($){
    $('.navbar').css("background-color", "transparent");
    $(window).scroll(function(){
        if ($(this).scrollTop() > 600) {
            $('.navbar').css("background-color", 
            "rgba(255,255,255,0.9)").css("transition","0.3s ease-in-out ");
            $('.navbar a').css("color", "black");
        } 
        else {
            $('.navbar').css("background-color", 
            "transparent").css("transition","0.3s ease-in-out ");
            $('.navbar a').css("color", "white");
        }
    });
});

and I could use some help with what code do I need to add for this code to run only on desktops (width min. 992px). I don't want this effect on mobile or tablet so I will appreciate any of your ideas!

Thank you!

2

1 Answer 1

0

If you define a desktop as having a screen with a resolution of over 992, then this would work. If you want to use user-agent to detect type of device, that's possible as well (and, arguably, more accurate). But give you are doing this only for styling, it shouldn't matter if your application is technically correct.

I'd recommend that you run this code or a version of it in the resize function, that way it is responsive to changing sizes of the browser's width.

$(document).ready(function($){
    $(document.body).resize(function () {
      var isDesktop = $(document).width() > 992;
      if (isDesktop) {
        $('.navbar').css("background-color", "transparent");
        $(window).scroll(function(){
            if ($(this).scrollTop() > 600) {
                $('.navbar').css("background-color", 
                "rgba(255,255,255,0.9)").css("transition","0.3s ease-in-out ");
                $('.navbar a').css("color", "black");
            } 
            else {
                $('.navbar').css("background-color", 
                "transparent").css("transition","0.3s ease-in-out ");
                $('.navbar a').css("color", "white");
            }
        });
      }
    }).resize();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.