getBoundingClientRect() 用于获取元素相对于浏览器饰扣的位置,这个API早已经是W3C标准兼容性不用担心,不过在IE下有些许的区别

{
  top: '元素顶部相对于视口顶部的距离',
  bottom: '元素底部相对于视口底部的距离',
  left: '元素左边相对于视口左边的距离',
  right: '元素右边相对于视口左边的距离',
  height: '元素高度',
  width: '元素宽度'
}

// IE兼容性写法
function getClientReat(client) {
    const { top, bottom, left, right, height, width } = client.getBoundingClientRect()
    return {
        top,
        bottom,
        left,
        right,
        height: height || right - left,
        width:    width || bottom - top
} }

兼容性参见 https://caniuse.com/#search=getBoundingClientRect

MDN: https://developer.mozilla.org/zh-CN/docs/Web/API/Element/getBoundingClientRect

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-12
  • 2021-09-17
  • 2021-06-27
  • 2021-06-01
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-05-30
  • 2021-10-29
  • 2022-02-05
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案