窗口大小调整事件之前和之后的 JavaScript

标签 javascript events window-resize will-change

onresize 可用于了解窗口大小已调整。窗口调整大小之前是否有任何类似的通知/事件。 我想在窗口调整大小开始之前对某些元素设置 will-change ,然后在窗口调整大小后将其删除。

最佳答案

似乎没有直接的解决方案,但您可以使用 window.addEventListener("resize",yourFunction); 通过一些解决方法来实现开始|结束事件和一些全局变量(这是一种解决方法,并不完美)。

//Accesible variables
var atStart = true;
var timer;

function yourFunction(){
    return ()=>{
        if(atStart){
          console.log("START");
          //Some code to be executed once at start
          atStart = false;
        }
        console.log("WHILE");
        //Some code to be executed while resizing

        if (timer) clearTimeout(timer);
        timer= setTimeout(atEnd, 500);
    }
}

function atEnd(){
    //Some code to be executed at the end of resizing
    //..well some 500ms after the end of resizing
    console.log("END")
    atStart = true;
}

window.addEventListener("resize",yourFunction());

希望它对某人有帮助。

关于窗口大小调整事件之前和之后的 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43003528/

相关文章:

javascript - 将 mooRainbow 转换为 mootools 1.4.4 时出现 bindWithEvent 错误

javascript - 如何在JS代码中订阅dijit事件?

javascript - 在页面加载时打开一个弹出窗口并在其他任何地方单击时隐藏 + 在调整窗口大小时动态跟随其父级

javascript - 使元素仅在窗口缩小时水平移动

javascript - 如何允许iframe访问Element.prototype

Java swt 改变键入的字符

javascript - 如何从 JavaScript 的 Math.random 生成包含边界的 Pareto 随机整数

javascript - jquery 就绪和调整大小功能不起作用

php - 根据下拉列表选择从数据库填充另一个选择下拉列表

javascript - 这个 "(function(){});"是括号内的函数,在 javascript 中是什么意思?