前端js获取标签尺寸

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type="text/css">
			*{
				margin: 0;
				padding: 0;
			}
			.box{
				width: 300px;
				height: 300px;
				margin: 50px;
				background-color: skyblue;
				border: 5px solid gold;
				padding: 20px;
			}
			.one{
				width: 200px;
				height: 200px;
				background-color: gray;
			}
		</style>
	</head>
	<body>
		<div class="box">
			<div class="one">
				
			</div>
		</div>
		
	</body>
</html>
<script type="text/javascript">
	//1.获取标签
	var box =document.querySelector(".box");
	var one=box.firstElementChild;
	/*
	 2.和标签宽高尺寸相关的属性
	 内尺寸:width/height+ adding
	 clientWidth=width+左右padding
	 clientHeight=height+上下padding
	 
	 外尺寸:width/height+padding+border
	 offsetWidth=width+左右padding+左右border
	 offsetHeight=height+上下padding+上下border
	 
	 注意:内外尺寸就只差了一个border
	 
	 */
	console.log("内尺寸:",box.clientWidth,box.clientHeight);
	console.log("外尺寸:",box.offsetWidth,box.offsetHeight);
	
	/*
	 3.和标签距左距上的距离相关的属性
	 offsetLeft 是距离定位所参考的父级的左边的距离 (不包含border)
	 offsettop 是距离定位所参考的父级的上面的距离 (不包含border)
	 如果没有定位 就是默认到浏览器窗口左边或上面的距离
	 
	 */
	console.log(box.offsetLeft);
	console.log(box.offsetTop);
	console.log("测试one");
	console.log(one.offsetLeft);
	console.log(one.offsetTop);
	//输出所定位参考的父标签
	console.log(one.offsetParent);
	//------------------------------
	/*
	 4.不常用的 了解即可
	 clientLeft: 获取标签做边框的宽度
	 clienttop: 获取上边框的宽度
	 */
	console.log("box:",box.clientLeft,box.clientTop);
	console.log("one:",one.clientLeft,one.clientTop);
</script>

运行结果:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值