Jiří Robenek Jiří Robenek - 3 months ago 15
jQuery Question

Combination of two conditions: scroll_pos and screen.width

I have got this code:

$(document).ready(function(){
var scroll_pos = 1;
$(document).scroll(function() {
scroll_pos = $(this).scrollTop();
if (scroll_pos < 5) && (screen.width > 480) {
$(".jumbotron").slideDown("slow");
$(".container").css("background-color", "transparent");
$(".topmenu").css("opacity", "0.5");
} else {
$(".jumbotron").slideUp("slow" );
$(".container").css("background-color", "#6c5147");
$(".topmenu").css("opacity", "1.0");
}
});
});


I need to achieve that script will not work if the screen is smaller than 480px. If I delete
&& (screen.width > 480)
the script works fine.

What am I doing wrong? Thanks for the advice, it's my first day with jQuery

Answer

The issue is with your if condition syntax - you need to wrap all conditions in one set of brackets. Try this:

if (scroll_pos < 5 && screen.width > 480) {