【JS】- 获取元素在浏览器中的位置

// 获取元素坐标 - left
getElementPageLeft(element) {
  var actualLeft = element.offsetLeft;
  var parent = element.offsetParent;
  while (parent != null) {
    actualLeft += parent.offsetLeft + parent.clientLeft;
    parent = parent.offsetParent;
  }
  return actualLeft;
},
// 获取元素坐标 - top
getElementPageTop(element) {
  var actualTop = element.offsetTop;
  var parent = element.offsetParent;
  while (parent != null) {
    actualTop += parent.offsetTop + parent.clientTop;
    parent = parent.offsetParent;
  }
  return actualTop;
}

  

posted @ 2023-02-01 09:29  迷你胡丶  阅读(202)  评论(0编辑  收藏  举报