javascript - why is jquery resize not working? -
i making responsive menu. put code change menu in function called file. when document or window resized jquery call function file again change menu. not work. when load page works on resize don't.
jquery:
var width = $(window).width(); file(); function file(){ if(width <= 400){ $('.link').remove(); $('head').append('<link class="link" rel="stylesheet" href="nav2.css">'); }else if(width >400){ $('.link').remove(); $('head').append('<link class="link" rel="stylesheet" href="nav.css">'); }else{ alert('error'); } } $(window).resize(function(){ file(); }); $(document).resize(function(){ file(); });
can me fixing problem
your width
variable isn't going change once it's set. should move inside function declaration.
function file(){ var width = $(window).width(); if(width <= 400){ $('.link').remove(); $('head').append('<link class="link" rel="stylesheet" href="nav2.css">'); }else if(width >400){ $('.link').remove(); $('head').append('<link class="link" rel="stylesheet" href="nav.css">'); }else{ alert('error'); } }
Comments
Post a Comment