2017-01-11 42 views
0

當窗口大小改變時,我只想重新加載一次。如何使用jQuery重新加載頁面和斷點?

當窗口大小發生變化時,您只需執行一次重新加載。

原因是,pc使用標籤和智能手機變成了手風琴。

if (window.matchMedia('screen and (max-width:767px)').matches) { 

location.reload(); 
} else if (window.matchMedia('screen and (min-width:768px)').matches) { 

location.reload(); 

} else { 

} 

回答

0

您可以使用setTimeout()

var resizeListener; 

//the amount of time to wait after the resizing has finished before calling our function 
var pause = 500; 

$(window).resize(function(){ 

    //every time the window resize is called cancel the setTimeout() function 
    clearTimeout(resizeListener); 

    //set out function to run after a specified amount of time 
    resizeListener = setTimeout(function(){ 
    alert("The user has done resizing the window"); 
    },pause); 

}); 

來源:https://thepixel.ninja/jquery/how-to-run-a-function-after-window-resize-using-jquery/