treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] jquery window resize fires only once

  • the resize even below actually hides a small fixed div when the size of the window is less than 800px.but it does it only for the first time u resize .

      $(document).ready(function(){
          if(window.innerWidth<800)
              $f=$(".hireus").detach();
    
          $(window).resize(function(){
              if(window.innerWidth<800)
                  $f=$(".hireus").detach();
          });
      });
    
  • It's firing every time your browser resizes - That's what $(window).resize() does. You can test that with a console.log('fired'); or something. Your script: $f=$(".hireus").detach(); is what is probably only firing once.

  • Well, you are only checking if the window size is less than 800, you need to also check for if it is greater. Here is a cleaner solution:

    
    jQuery(document).ready(function($) {
      if(window.innerWidth<800) {
          $(".hideme").hide();
      }
      $(window).resize(function(){
          if(window.innerWidth<800) {
              $(".hideme").hide();
          } else if(window.innerWidth>800) {
          $(".hideme").show();
          }
      });
    });
    
  • oh yeah that's right .. i tried it out and it works.But the problem comes when i try to change the font sizes when there is a window resize or change in screen size..