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

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -