Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
111
rated 0 times [  118] [ 7]  / answers: 1 / hits: 17661  / 8 Years ago, sun, august 7, 2016, 12:00:00

I did a function where I checked if the window has been resized and an element is visible in order to change some classes and add a little bit of css .The problem is if I refresh the page the changes are reverted . Here is a snippet of my code :



$(document).ready(function() {

$(window).resize(function() {
if(isMenuVisible() == true){
$('#my-nav').removeClass('navbar-fixed-bottom');
$('#my-nav').addClass('navbar-fixed-top');
$('body').css('padding-top', '50px');
} else{
$('#my-nav').removeClass('navbar-fixed-top');
$('#my-nav').addClass('navbar-fixed-bottom');
$('body').css('padding-top', '0');
}
}) ;
});

More From » jquery

 Answers
43



$(document).ready(function() {  

function resizeChanges(){
if(isMenuVisible() == true){
$('#my-nav').removeClass('navbar-fixed-bottom');
$('#my-nav').addClass('navbar-fixed-top');
$('body').css('padding-top', '50px');
}else{
$('#my-nav').removeClass('navbar-fixed-top');
$('#my-nav').addClass('navbar-fixed-bottom');
$('body').css('padding-top', '0');
}
}

$(window).resize(resizeChanges);

resizeChanges();
});





This way you define a separate function to make your changes, you add the event listener to trigger it but also you call that function itself after the load/refresh.


[#61115] Thursday, August 4, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jazminkyrap

Total Points: 631
Total Questions: 89
Total Answers: 109

Location: Finland
Member since Fri, Oct 21, 2022
2 Years ago
;