获取dom位置信息

 
        
getDomPosition (dom) {
            if (dom) {
                return Array.from(dom).map(node => {
                    let style = null
                    if (window.getComputedStyle) {
                        style = window.getComputedStyle(node, null) // 非IE
                    } else {
                        style = node.currentStyle // IE
                    }
                    const { top: t, left: l } =
                        node.getBoundingClientRect() || {}
                    // const top = parseInt(node.offsetTop, 10);     //相对于父元素
                    // const left = parseInt(node.offsetLeft, 10);   //相对于父元素
                    const top = parseInt(t, 10) // 相对于窗口
                    const left = parseInt(l, 10) // 相对于窗口
                    const width = parseInt(style.width, 10)
                    const height = parseInt(style.height, 10)
                    return { left, top, width, height }
                })
            } else {
                return []
            }
        },

 

posted @ 2020-09-18 17:36  栀夏。  阅读(274)  评论(0编辑  收藏  举报