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;
             }

posted on 2012-09-21 10:23  浩瀚孤鸿  阅读(215)  评论(0编辑  收藏  举报