关键API:

mdn:https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect

 

用法:

var rect = element.getBoundingClientRect();
console.log(rect.top, rect.right, rect.bottom, rect.left);

 

获取相对位置:

var bodyRect = document.body.getBoundingClientRect(),
    elemRect = element.getBoundingClientRect(),
    offset   = elemRect.top - bodyRect.top;

alert('Element is ' + offset + ' vertical pixels from <body>');

 

参考网址:http://stackoverflow.com/questions/442404/retrieve-the-position-x-y-of-an-html-element

 

我只是个搬运工,一起学习了~

相关文章:

  • 2021-11-04
  • 2022-12-23
  • 2021-10-16
  • 2022-12-23
  • 2022-12-23
  • 2021-09-19
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-31
  • 2021-10-04
  • 2021-06-03
  • 2021-11-04
  • 2021-11-14
相关资源
相似解决方案