Reputation: 939
I have following code in my page. Background color works perfectly. But I have to refresh to see the change regarding li. If I don't refresh, li has previous effect. Such as in windows screen smaller 500 px, li is shown without clicking.
function myFunction() {
if ($(window).width() < 500) {
$('body').css('background', 'black');
$("nav").click(function() {
$("li").toggle('slow');
});
} else {
$('body').css('background', 'blue');
$("li").show();
}
}
Upvotes: 0
Views: 357
Reputation: 2261
Put your code as a function and call it on onload and on window resize like below
$(window).load(function(){
//your function
});
$(window).resize(function(){
//your function
});
Upvotes: 1
Reputation: 8249
$("nav").click(function() {
$("li").toggle('slow');
});
function myFunction() {
if ($(window).width() < 500) {
$('body').css('background-color', 'black');
} else {
$('body').css('background-color', 'blue');
$("li").show();
}
}
background
, you should background-color
click
outside on the function rather than defining it inside.myFunction
is triggered/called on resize
or load
Upvotes: 1
Reputation: 1586
You need to trigger it also on resize, so if you resize the window it will trigger the queries like
$(window).on("load resize",function(){
//your code goes here
if ($(window).width() < 500) {
$('body').css('background', 'black');
$("nav").click(function() {
$("li").toggle('slow');
});
} else {
$('body').css('background', 'blue');
$("li").show();
}
}
Upvotes: 1