如何通过JS获取元素宽高

方法一:DOM节点.offsetWidth/offsetHeight
作用:获取到的是内容区域(content) + 内边距(padding) + 边框(border)的值,输出一个数字。

    <style type="text/css">
        div{
            width: 100px;
            height: 100px;
            padding: 10px;
            border: 10px solid blue;
            margin: 10px;
        }
    </style>

<body>
    <div></div>
</body>

<script>
    var dom = document.getElementsByTagName("div")[0];
    console.log(dom.offsetWidth);       //140
    console.log(dom.offsetHeight);      //140
</script>

方法二:DOM节点.clientWidth/clientHeight
作用:获取到的是内容区域(content) + 内边距(padding)的值,输出一个数字。

    <style type="text/css">
        div{
            width: 100px;
            height: 100px;
            padding: 10px;
            border: 10px solid blue;
            margin: 10px;
        }
    </style>

<body>
    <div></div>
</body>

<script>
    var dom = document.getElementsByTagName("div")[0];
    console.log(dom.clientWidth);       //120
    console.log(dom.clientHeight);      //120
</script>

方法三:DOM节点.style.width/height
作用:获取内联样式设置的宽高;
缺点:获取不到嵌入样式和外部样式。

    <style type="text/css">
        div{
            height: 100px;
        }
    </style>

<body>
    <div style="width:100px;"></div>
</body>

<script>
    var dom = document.getElementsByTagName("div")[0];
    console.log(dom.style.width);       //100px
    console.log(dom.style.height);      //""
</script>

方法四:DOM节点.currentStyle.width/height
作用:获取元素渲染之后的宽高;
缺点:只能在IE浏览器使用。

    <style type="text/css">
        div{
            height: 100px;
            width: 100px;
        }
    </style>

<body>
    <div></div>
</body>

<script>
    var dom = document.getElementsByTagName("div")[0];
    console.log(dom.currentStyle.width);       //100px
    console.log(dom.currentStyle.height);      //100px
</script>

方法五:window.getComputedStyle(DOM节点).width/height
作用:获取元素宽高的最终计算值;
缺点:不兼容IE8及以下版本。

    <style type="text/css">
        div{
            height: 100px;
            width: 100px;
        }
    </style>

<body>
    <div></div>
</body>

<script>
	var dom = document.getElementsByTagName("div")[0];
	console.log(window.getComputedStyle(dom,null).width);		//100px
	console.log(window.getComputedStyle(dom,null).height);		//100px
</script>

方法六:DOM节点.getBoundingClientRect().width/height
作用:获取到的是内容区域(content) + 内边距(padding) + 边框(border)的值,输出一个数字。

    <style type="text/css">
        div{
            height: 100px;
            width: 100px;
            padding: 10px;
            border: 10px solid blue;
            margin: 10px;
        }
    </style>

<body>
    <div></div>
</body>

<script>
	var dom = document.getElementsByTagName("div")[0];
	console.log(dom.getBoundingClientRect().width);			//140
	console.log(dom.getBoundingClientRect().height);		//140
</script>
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值