有没有一种方法可以让我得到window.onresize只刷新页面的宽度大小

Is there a way for me to get window.onresize to only refresh the page for width resizes only?

本文关键字:刷新 onresize window 一种 方法 有没有      更新时间:2023-09-26

我想让下面这样的东西只在窗口宽度(而不是高度)发生变化时起作用:

window.onresize = function(event)
{
   document.location.reload(true);
}

我已经尝试过做一些研究,包括在这里寻找一些答案,但无论我做什么来调整我发现的代码都不起作用。我该怎么办?

你可以试试这样的东西:

var w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0],
x = w.innerWidth || e.clientWidth || g.clientWidth;
window.onresize = function(event){
    var t = w.innerWidth || e.clientWidth || g.clientWidth;
    if(t !== x) {
       document.location.reload(true);
    }
}