窗口宽度小于767px时如何刷新页面

时间:2014-07-15 08:22:18

标签: jquery resize

如何在调整窗口大小小于767px后刷新页面一次?

我尝试了很多例子,但效果不好。

$(document).ready(function(){

    if ($(window).width() < 767) {   

             location.reload();  // refresh page 

    }
    else {  

            // width more than 768px for PC  

    }
}); 

请帮助〜

2 个答案:

答案 0 :(得分:3)

$(document).ready(function(){
$(window).on('resize',function(){
    if ($(window).width() < 767) {   
      location.reload();  // refresh page 
    }
    else {  
      // width more than 768px for PC  
    }
}); 
});

答案 1 :(得分:0)

如果不想早点重新加载,你可能会这样延迟:

$(document).ready(function() {
    $(window).on("resize", function() {
        var tm;
        clearTimeout(tm);
        tm = setTimeout(function() {
            if ($(window).width() < 767) {
                location.reload();  // refresh page 
            }
            else {
                // width more than 768px for PC  
            }
        }, 100);
    });
});