Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript,一种跨平台的方法,用于处理仅在发生时调整大小的事件_Javascript_Html_Cross Browser_Cross Platform - Fatal编程技术网

Javascript,一种跨平台的方法,用于处理仅在发生时调整大小的事件

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

我想在当前窗口的大小改变时执行一个函数

我有一个非常简单的代码:(foo.html)


但在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...
}