Element.getBoundingClientRect()

Element.getBoundingClientRect()

理解: getBoundingClientRect() 用于获取某个元素相对于视窗的位置集合.

该Element.getBoundingClientRect()方法返回一个Object对象,该对象有6个属性: top, bottom, left, right, width, height;
这里的top left和css中的理解很相似,width height是元素自身的宽高,但right bottom和css中的理解有点不一样. right是指元素右边界距窗口最左边的距离,bottom是指元素下边界距窗口最上面的距离.

句法:

domRect = element.getBoundingClientRect();

在这里插入图片描述

 if (document.documentElement.getBoundingClientRect) { 
 	alert("left:"+this.getBoundingClientRect().left);
    alert("top:"+this.getBoundingClientRect().top)
    alert("right:"+this.getBoundingClientRect().right)
    alert("bottom:"+this.getBoundingClientRect().bottom)
    var X= this.getBoundingClientRect().left+document.documentElement.scrollLeft;
 	var Y = this.getBoundingClientRect().top+document.documentElement.scrollTop;
   	alert("Demo的位置是X:"+X+";Y:"+Y)
 } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值