我已经尝试过这个,它只是在我的浏览器控制台中带来了一个空白空间。 我怎样才能得到我的元素的高度。
var background = document.getElementById('bgimage');
console.log(background.style.height);
.background {
width: 100%;
height: 500vh;
}
<div class="section s1">
<div class="background" id="bgimage">
.......
</div>
</div>
最佳答案
使用offsetHeight
:
var background = document.getElementById('bgimage');
var h = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
console.log(background.offsetHeight*100/h);
.background {
width: 100%;
height: 500vh;
}
<div class="section s1">
<div class="background" id="bgimage">
.......
</div>
</div>
注意:element.offsetHeight
将返回 element
的高度(以像素 (px
) 为单位)。
关于javascript - 我怎样才能在javascript中获取元素高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59115063/