获取浏览器及Dom宽高

参考文章:

https://blog.csdn.net/qq_36947128/article/details/80324538

http://www.cnblogs.com/jin-zhe/p/10506238.html

https://www.cnblogs.com/jyybeam/p/6183663.html

以元素的高度为例(宽度同理),一般会用到下边四种获取高度:

height、offsetHeight、clientHeight、innerHeight

<style>
 .test{
    box-sizing:content-box;
    width:40px;
    height:30px;
    padding:5px;
    margin:5px;
    border:1px solid #000;
    background:#f0f;
}
</style>

<div class="test" id="test" style="height:50px;">test</div>
<div class="test" id="test1" >test1</div>
//获取DOM元素内联高度的值
var height = document.getElementById('test').style.height;
var height1 = document.getElementById('test1').style.height;

//offsetHeight= DOM元素高 + padding + border
 var offsetHeight = document.getElementById('test').offsetHeight;

//clientHeight= DOM元素高 + padding 
var clientHeight = document.getElementById('test').clientHeight;

//DOM无innerHeight属性
var DomInnerHeight = document.getElementById('test').innerHeight;

//Window才有innerHeight属性
var WindowInnerHeight = window.innerHeight;
                
console.log('height:'+height+',',
            'height1:'+height1+',',
            'offsetHeight:'+offsetHeight+',',
            'clientHeight:'+clientHeight+',',
            'DomInnerHeight:'+DomInnerHeight+',',
            'WindowInnerHeight:'+WindowInnerHeight);
//console
//height:50px, height1:, offsetHeight:62, clientHeight:60, DomInnerHeight:undefined, WindowInnerHeight:463

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值