Chrome 调整大小事件和 getComputedStyle
Chrome Resize event and getComputedStyle
我对调整大小事件和 getComputedStyle 方法有疑问。当我将 window 从例如 700px 调整到 800+ px 时,我的元素的高度从 79px 更改为默认值(10px)。但是,当我想通过 getComputedStyle 获取此值 (10px) 或在 devtools 中检查它时,会输出如下内容:76、63、59,最后是 10px。
所以 devtools 中的高度值在下一个调整大小事件时更新,而不是第一次,并且在每个调整大小事件之间有一些中间值。
所以我的问题是,如何获得最终价值?我尝试使用 setTimeout 方法调整大小,但这也没有像我预期的那样工作。我的代码看起来是这样的:
window.addEventListener("DOMContentLoaded", function(){
if(window.matchMedia("(min-width:800px)").matches){
default();
}
else{
changeHeight();
}
window.addEventListener('resize',onResizeFunction);
})
function default(){
slot.parentElement.style.height=''
console.log(slot.parentElement.style.height)
console.log(getComputedStyle(slot.parentElement).height) //problem with this line, when called in resize event it's output something like this: 76,63,59 and then 10px;
}
function changeHeight(){
slot.parentElement.style.height="79px";
console.log(slot.parentElement.style.height) //this is ok
}
function onResizeFunction(){
if(window.matchMedia("(min-width:800px)").matches){
default();
}
else{
changeHeight();
}
}
而且我不能使用CSS,因为我正在做一个旧项目,只能使用 JS 来完成这项工作
请为你的 dom 提供你的 css,我打赌你会有一些转变
我对调整大小事件和 getComputedStyle 方法有疑问。当我将 window 从例如 700px 调整到 800+ px 时,我的元素的高度从 79px 更改为默认值(10px)。但是,当我想通过 getComputedStyle 获取此值 (10px) 或在 devtools 中检查它时,会输出如下内容:76、63、59,最后是 10px。 所以 devtools 中的高度值在下一个调整大小事件时更新,而不是第一次,并且在每个调整大小事件之间有一些中间值。 所以我的问题是,如何获得最终价值?我尝试使用 setTimeout 方法调整大小,但这也没有像我预期的那样工作。我的代码看起来是这样的:
window.addEventListener("DOMContentLoaded", function(){
if(window.matchMedia("(min-width:800px)").matches){
default();
}
else{
changeHeight();
}
window.addEventListener('resize',onResizeFunction);
})
function default(){
slot.parentElement.style.height=''
console.log(slot.parentElement.style.height)
console.log(getComputedStyle(slot.parentElement).height) //problem with this line, when called in resize event it's output something like this: 76,63,59 and then 10px;
}
function changeHeight(){
slot.parentElement.style.height="79px";
console.log(slot.parentElement.style.height) //this is ok
}
function onResizeFunction(){
if(window.matchMedia("(min-width:800px)").matches){
default();
}
else{
changeHeight();
}
}
而且我不能使用CSS,因为我正在做一个旧项目,只能使用 JS 来完成这项工作
请为你的 dom 提供你的 css,我打赌你会有一些转变