Get position of element by JavaScript

后端 未结 4 883
南笙
南笙 2020-12-13 21:43

I\'ve seen dozens of scripts that can catch the x and y position of an element/object within the page. But I am always having trouble with catching the x and y when the webp

4条回答
  •  长情又很酷
    2020-12-13 22:29

    I use following code to move div box to follow cursor in this Web IME site

    function xy(x) {
        o = document.getElementById(x);
        var l =o.offsetLeft; var t = o.offsetTop;
        while (o=o.offsetParent)
            l += o.offsetLeft;
        o = document.getElementById(x);
        while (o=o.offsetParent)
            t += o.offsetTop;
        return [l,t];
    }
    

    Its return an array [left,top],

提交回复
热议问题