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/