Javascript,一种跨平台的方法,用于处理仅在发生时调整大小的事件
我想在当前窗口的大小改变时执行一个函数 我有一个非常简单的代码:(foo.html)Javascript,一种跨平台的方法,用于处理仅在发生时调整大小的事件,javascript,html,cross-browser,cross-platform,Javascript,Html,Cross Browser,Cross Platform,我想在当前窗口的大小改变时执行一个函数 我有一个非常简单的代码:(foo.html) 但在Firefox(Android)上,我保留了同样的问题。当我加载页面时,会显示警报“FOO”。(在智能手机上,我只想在用户从纵向模式切换到横向模式时执行我的代码)为什么不这样做呢 var CurrentWindowSize = { w: document.body.clientWidth, h: document.body.clientHeight }; // only Chrome has corre
但在Firefox(Android)上,我保留了同样的问题。当我加载页面时,会显示警报“FOO”。(在智能手机上,我只想在用户从纵向模式切换到横向模式时执行我的代码)为什么不这样做呢
var CurrentWindowSize = { w: document.body.clientWidth, h: document.body.clientHeight }; // only Chrome has correct values here
window.onresize = function(e)
{
let width = e.target.outerWidth
, height = e.target.outerHeight
;
if (width != CurrentWindowSize.w
|| height != CurrentWindowSize.h)
{
CurrentWindowSize.w = width;
CurrentWindowSize.h = height;
WindowIsResized();
}
}
function WindowIsResized() {
// your stuff...
}
PS:最好放在结束标记之前。之后没有。我刚刚测试过,我在Chrome(桌面)和Firefox(Android)上也有同样的问题。为什么
setTimeout()
如果你只想让它出现一次,为什么不将一个变量设置为false
并在你的resize eventlistener函数中更改它呢?@NewToJS:你是对的,但这不是重点。我想解决安卓问题你的HTML
或CSS
中有没有改变移动设备元素的东西?如果是这样的话,您是否可以添加一些HTML
和CSS
的示例,以在一个小示例/演示中重现该问题?非常感谢。
var resizeTimer;
window.onresize = function(){
if (resizeTimer){
clearTimeout(resizeTimer);
}
resizeTimer = setTimeout(function(){
alert("FOO");
}, 50);
};
var CurrentWindowSize = { w: document.body.clientWidth, h: document.body.clientHeight }; // only Chrome has correct values here
window.onresize = function(e)
{
let width = e.target.outerWidth
, height = e.target.outerHeight
;
if (width != CurrentWindowSize.w
|| height != CurrentWindowSize.h)
{
CurrentWindowSize.w = width;
CurrentWindowSize.h = height;
WindowIsResized();
}
}
function WindowIsResized() {
// your stuff...
}