js准确获取弹窗显示的位置

js准确获取弹窗显示的位置_第1张图片

let objTop = getOffsetTop(document.getElementById("work-area")); //对象x位置
let objLeft = getOffsetLeft(document.getElementById("work-area")); //对象y位置
                let mouseX = e.clientX + document.body.scrollLeft; //鼠标x位置
                let mouseY = e.clientY + document.body.scrollTop; //鼠标y位置
                let objX = mouseX - objLeft;
                let objY = mouseY - objTop;
                self.mouseMenuPos.left = objX + scollPostion().left;
                self.mouseMenuPos.top = objY + scollPostion().top;

                function scollPostion() {
                    let t, l, w, h;
                    if (document.documentElement && document.documentElement.scrollTop) {
                        t = document.documentElement.scrollTop;
                        l = document.documentElement.scrollLeft;
                        w = document.documentElement.scrollWidth;
                        h = document.documentElement.scrollHeight;
                    } else if (document.body) {
                        t = document.body.scrollTop;
                        l = document.body.scrollLeft;
                        w = document.body.scrollWidth;
                        h = document.body.scrollHeight;
                    }
                    return {
                        top: t,
                        left: l,
                        width: w,
                        height: h
                    };
                }

                function getOffsetTop(obj) {
                    let tmp = obj.offsetTop;
                    let val = obj.offsetParent;
                    while (val != null) {
                        tmp += val.offsetTop;
                        val = val.offsetParent;
                    }
                    return tmp;
                }

                function getOffsetLeft(obj) {
                    let tmp = obj.offsetLeft;
                    let val = obj.offsetParent;
                    while (val != null) {
                        tmp += val.offsetLeft;
                        val = val.offsetParent;
                    }
                    return tmp;
                }

 

你可能感兴趣的:(js准确获取弹窗显示的位置)