美文网首页
如何计算元素的top和left

如何计算元素的top和left

作者: 东方灵将 | 来源:发表于2018-04-11 21:50 被阅读0次
  • 以百度搜索框为例
let search = document.getElementById('kw');
let rect = search.getBoundingClientRect();
let bodyRect = document.body.getBoundingClientRect();
let top = rect.top - bodyRect.top
let left = rect.left - bodyRect.left

相关文章

网友评论

      本文标题:如何计算元素的top和left

      本文链接:https://www.haomeiwen.com/subject/gjftkftx.html