Link my scrollTop() method to media queries via window.matchMedia

167 Views Asked by At

Hy, i'm new in js. I want to link my scrollTOp method to media queries via JS (window.matchMedia).

<script>
$(document).ready(function(){
            $(window).scroll(function() { 
                if ($(document).scrollTop() > 458) { 
                    $(".menu a").css("background-color", "#f8f8f8"); 
                    $(".menu").css("display", "none");                    
                    $(".sidenav").css("display", "block");
                    $(".closebtn").css("display", "block");
                } 
else {
                $(".menu a").css("background-color", "#666");
                $(".menu").css("display", "block"); 
                $(".sidenav").css("display", "none");
                $(".closebtn").css("display", "none");
            }
            });
        });
</script>

I don't know how, please help :)

1

There are 1 best solutions below

0
On

If I have understood correctly, you want to check if your $(document).scrollTop() > 458 and if it matches a media query.

To do that you can simply add :

if ( $(document).scrollTop() > 458 && window.matchMedia("MEDIA_QUERY_HERE").matches){
    ....
}

More informations about window.matchMedia here and about the logical 'and' operator here.