jquery 获取元素坐标

绝对X,Y坐标,可以用offset()方法:

1 2
var X = $('#DivID').offset().top; var Y = $('#DivID').offset().left;

获取相对(父元素)位置:

1 2
var X = $('#DivID').position().top; var Y = $('#DivID').position().left;


             function getTop(e)
             {
             var offset = e.offsetTop;
             if (e.offsetParent != null)
             offset += getTop(e.offsetParent);
             return offset;
             }
             
             // 获取元素的横坐标
             function getLeft(e)
             {
             var offset = e.offsetLeft;
             if (e.offsetParent != null)
             offset += getLeft(e.offsetParent);
             return offset;
             }

原文地址:https://www.cnblogs.com/webu/p/2696469.html