获取html元素所在页面的坐标

function findPosition(oElement) {
            var x2 = 0;
            var y2 = 0;
            var width = oElement.offsetWidth;
            var height = oElement.offsetHeight;
            if (typeof (oElement.offsetParent) != 'undefined') {
                for (var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent) {
                    posX += oElement.offsetLeft;
                    posY += oElement.offsetTop;
                }
                x2 = posX + width;
                y2 = posY + height;
                //return [ posX, posY ,x2, y2];
                return { x: posX, y: posY, xx: x2, yy: y2 };
            } else {
                x2 = oElement.x + width;
                y2 = oElement.y + height;
                return { x: oElement.x, y: oElement.y, xx: x2, yy: y2 };
            }
        }

  此方法获取到的是json格式的数据,包括元素左上角的坐标和元素的宽高。

posted @ 2013-10-20 20:26  stevejson  阅读(1779)  评论(0编辑  收藏  举报