html中隐藏div的高度,如何获取隐藏div元素的高度

使用Element.getBoundingClientRect()来获取相对于viewport的位置,

2个元素top位置的差就是偏移量

Title

body{

padding: 0;

margin: 0;

width: 100vw;

height: 100vh;

}

.container{

position: absolute;

left:10px;

top:10px;

width: 500px;

height: 500px;

background-color: #ffffff;

box-sizing: border-box;

border:1px solid #cccccc;

overflow: hidden;

}

.child{

width: 100%;

height: 200%;

background-color: red;

border: 1px solid red;

box-sizing: border-box;

}

.calcButton{

position: absolute;

left:0;

bottom:0;

width: 100px;

height: 50px;

box-sizing: border-box;

}

.child-wrapper{

position: absolute;

left:0;

right: 0;

top:0;

bottom:50px;

overflow-x: hidden;

overflow-y: auto;

}

计算

document.querySelector(".calcButton").addEventListener("click",function(){

var childWrapperRect=document.querySelector(".child-wrapper") .getBoundingClientRect();

var childRect=document.querySelector(".child") .getBoundingClientRect();

this.innerText="当前偏移-"+(childWrapperRect.top-childRect.top);

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值