刷新网站时最大化并按下还原按钮

时间:2019-01-04 11:08:41

标签: jquery window-resize

我想在用户缩小浏览器窗口时重新加载网站。我已经开始使用它了,但是当您缩小浏览器窗口的大小后按最大化按钮时,它不会恢复正常,而是停留在移动视图中。

我找到了一种可以工作的代码,但不是100%。 如果有人可以帮助我,我将不胜感激。我真的是javascript / jQuery的新手。下面是jQuery代码,我觉得它不是最好看的,但是可以用。

$(window).on('resize', function(event) {
  var windowSize = $(window).width(); // Could've done $(this).width()
  //Smartphones, Portrait iPhone, Portrait 480x320 Phones (Android)
  if (windowSize < 320) {
    //Refresh page on browser resize
    $(window).bind('resize', function(e) {
      if (window.RT) clearTimeout(window.RT);
      window.RT = setTimeout(function() {
        this.location.reload(false); /* False to get page from cache */
      }, 200);
    });

    //Smartphones, Android Phones, Landscape iPhone
  } else if (windowSize < 480) {
    //Refresh page on browser resize
    $(window).bind('resize', function(e) {
      if (window.RT) clearTimeout(window.RT);
      window.RT = setTimeout(function() {
        this.location.reload(false); /* False to get page from cache */
      }, 200);
    });

  }
  //Portrait Tablets, Portrait iPad, E-readers (Nook/Kindle), Landscape 800x480 Phones (Android)
  else if (windowSize < 600) {
    //Refresh page on browser resize
    $(window).bind('resize', function(e) {
      if (window.RT) clearTimeout(window.RT);
      window.RT = setTimeout(function() {
        this.location.reload(false); /* False to get page from cache */
      }, 200);
    });

  } else if (windowSize < 690) {
    //Refresh page on browser resize
    $(window).bind('resize', function(e) {
      if (window.RT) clearTimeout(window.RT);
      window.RT = setTimeout(function() {
        this.location.reload(false); /* False to get page from cache */
      }, 200);
    });

  }
  //Tablet, Landscape iPad, Lo-res Laptops and Desktops
  else if (windowSize < 801) {
    //Refresh page on browser resize
    $(window).bind('resize', function(e) {
      if (window.RT) clearTimeout(window.RT);
      window.RT = setTimeout(function() {
        this.location.reload(false); /* False to get page from cache */
      }, 200);
    });

  } else if (windowSize < 995) {
    //Refresh page on browser resize
    $(window).bind('resize', function(e) {
      if (window.RT) clearTimeout(window.RT);
      window.RT = setTimeout(function() {
        this.location.reload(false); /* False to get page from cache */
      }, 200);
    });

  }
  //Big Landscape Tablets, Laptops, and Desktops 
  else if (windowSize < 1025) {
    //Refresh page on browser resize
    $(window).bind('resize', function(e) {
      if (window.RT) clearTimeout(window.RT);
      window.RT = setTimeout(function() {
        this.location.reload(false); /* False to get page from cache */
      }, 200);
    });

  }
  //Hi-res Laptops and Desktops 
  else if (windowSize < 1281) {
    //Refresh page on browser resize
    $(window).bind('resize', function(e) {
      if (window.RT) clearTimeout(window.RT);
      window.RT = setTimeout(function() {
        this.location.reload(false); /* False to get page from cache */
      }, 200);
    });

  } else if (windowSize < 1440) {
    //Refresh page on browser resize
    $(window).bind('resize', function(e) {
      if (window.RT) clearTimeout(window.RT);
      window.RT = setTimeout(function() {
        this.location.reload(false); /* False to get page from cache */
      }, 200);
    });

  } else if (windowSize > 1440) {
    //Refresh page on browser resize
    $(window).bind('resize', function(e) {
      $('body').prepend('<div>' + $(window).width() + '</div>');
      if (window.RT) clearTimeout(window.RT);
      window.RT = setTimeout(function() {
        this.location.reload(false); /* False to get page from cache */
      }, 10);
    });

  } else {
    $('html').css('color', 'black');
  }
});

1 个答案:

答案 0 :(得分:0)

所以我现在大部分工作了。但是我说1050px时,它会不断更新。有人知道精确匹配的分辨率是否有可能更新?我要用===做到这一点,但是不。

jQuery:

$(window).on('resize', function(event) {
     var windowSize = $(window).width(); // Could've done $(this).width()
     if ($('.toggle-menu').width() === 1599) {
      //Refresh page on browser resize
      $(window).bind('resize', function(e) {
       if (window.RT) clearTimeout(window.RT);
       window.RT = setTimeout(function() {
        this.location.reload(false); /* False to get page from cache */
       }, 1000);
      });
     } else if ($('.toggle-menu').width() === 1300) {
      //Refresh page on browser resize
      $(window).bind('resize', function(e) {
       if (window.RT) clearTimeout(window.RT);
       window.RT = setTimeout(function() {
        this.location.reload(false); /* False to get page from cache */
       }, 1000);
      });
     } else if ($('.toggle-menu').width() === 820) {
      //Refresh page on browser resize
      $(window).bind('resize', function(e) {
       if (window.RT) clearTimeout(window.RT);
       window.RT = setTimeout(function() {
        this.location.reload(false); /* False to get page from cache */
       }, 1000);
      });
     } else if ($('.toggle-menu').width() === 821) {
      //Refresh page on browser resize
      $(window).bind('resize', function(e) {
       if (window.RT) clearTimeout(window.RT);
       window.RT = setTimeout(function() {
        this.location.reload(false); /* False to get page from cache */
       }, 1000);
      });
     } else if ($('.toggle-menu').width() === 1024) {
      //Refresh page on browser resize
      $(window).bind('resize', function(e) {
       if (window.RT) clearTimeout(window.RT);
       window.RT = setTimeout(function() {
        this.location.reload(false); /* False to get page from cache */
       }, 1000);
      });
     } else if ($('.toggle-menu').width() === 900) {
      //Refresh page on browser resize
      $(window).bind('resize', function(e) {
       if (window.RT) clearTimeout(window.RT);
       window.RT = setTimeout(function() {
        this.location.reload(false); /* False to get page from cache */
       }, 1000);
      });
     } else if ($('.toggle-menu').width() === 768) {
      //Refresh page on browser resize
      $(window).bind('resize', function(e) {
       if (window.RT) clearTimeout(window.RT);
       window.RT = setTimeout(function() {
        this.location.reload(false); /* False to get page from cache */
       }, 1000);
      });
     } else if ($('.toggle-menu').width() === 769) {
      //Refresh page on browser resize
      $(window).bind('resize', function(e) {
       if (window.RT) clearTimeout(window.RT);
       window.RT = setTimeout(function() {
        this.location.reload(false); /* False to get page from cache */
       }, 1000);
      });
     }
       else if ($('.toggle-menu').width() === 1050) {
      //Refresh page on browser resize
      $(window).bind('resize', function(e) {
       if (window.RT) clearTimeout(window.RT);
       window.RT = setTimeout(function() {
        this.location.reload(false); /* False to get page from cache */
       }, 1000);
      });
     }
    });
//Update the website when you press the maximize button
$(window).resize( function(e) {
   if (window.RT) clearTimeout(window.RT);
   window.RT = setTimeout(function() {
      this.location.reload(false); /* False to get page from cache */
   }, 10);
});