javascript - Chrome Resize 事件和 getComputedStyle
问题描述
我对调整大小事件和 getComputedStyle 方法有疑问。当我将窗口大小从例如 700 像素调整到 800 像素以上时,我的元素高度从 79 像素变为默认值(10 像素)。但是当我想通过 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,我打赌您有一些过渡
推荐阅读
- angular - 如何在 ionic 4 中调用 sms api?
- amazon-web-services - AWS 侦听器 cf 模板在 aws cli 中给出错误,但在控制台中有效(设计模板)
- azure - 将对 Azure 应用程序 (WebApi) 的访问限制为仅对另一个 Azure 应用程序(守护程序客户端)
- arrays - 无法将while循环中的项目附加到bash中的空数组
- php - 寻找可靠的方法来知道我的手机是否在家
- c# - 减去不同的第 1 列和第 2 列的数据,在第 3 列显示其答案
- flutter - 我无法在颤振视频播放器中从 http 路径流式传输视频
- reactjs - 如何调试 React 插件
- oracle - 按日期范围对多列中的值求和
- installation - Inno Setup:复制文件(到 System32)需要很长时间