获取dom top left width heigth方法

export const getOffset = $target => {
    if(!$target) return false;
    /**
     * getBoundingClientRect是获取元素相对于视口的坐标信息的方法
     * window.pageXOffset 获取文档当前上下卷起的高度
     * window.pageXOffset 获取文档当前左右卷起的高度
     */
    const obj = $target.getBoundingClientRect()
    return {
      left: obj.left + window.pageXOffset,
      top: obj.top + window.pageYOffset,
      width: Math.round(obj.width),
      height: Math.round(obj.height)
    }
    
}

 

posted on 2019-09-20 10:10  KyleLjc  阅读(248)  评论(0编辑  收藏  举报

导航