javascript: Element.getBoundingClientRect() 获取元素在网页上的坐标位置

来自:https://blog.csdn.net/weixin_42895400/article/details/81811095?utm_source=blogxgwz1

Element.getBoundingClientRect() 方法返回元素的大小及其相对于视口的位置。

相关的属性:left,top,right,bottom

如果网页窗口有滚动条,还要加上下面的这一段:

var scrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
var scrollY = document.documentElement.scrollTop || document.body.scrollTop;

猜你喜欢

转载自www.cnblogs.com/personnel/p/10087468.html