获取元素距离文档顶部和左边的距离

2019-08-15  本文已影响0人  huanghaodong
function offset(element) {
  var pos = { left: 0, top: 0 }

  var parents = element.offsetParent

  pos.left += element.offsetLeft
  pos.top += element.offsetTop

  while (parents && !/html|body/i.test(parents.tagName)) {
    pos.left += parents.offsetLeft
    pos.top += parents.offsetTop

    parents = parents.offsetParent
  }
  return pos
}
上一篇下一篇

猜你喜欢

热点阅读