获取元素和鼠标的位置(兼容IE6.0,IE7.0,IE8.0,FireFox2.0,FireFox3.5,Opera)

 获取元素和鼠标的位置(兼容IE6.0,IE7.0,IE8.0,FireFox2.0,FireFox3.5,Opera),该功能是我一同事钻研出来的,目标是为了实现与QQ自定义布局和拖放模块类似的功能。

//获取元素的位置
    function getLeft(obj) {
        if (obj == null)
            return null;
        var mendingObj = obj;
        var mendingLeft = mendingObj.offsetLeft;
        while (mendingObj != null && mendingObj.offsetParent != null && mendingObj.offsetParent.tagName != "BODY") {
            mendingLeft = mendingLeft + mendingObj.offsetParent.offsetLeft;
            mendingObj = mendingObj.offsetParent;
        }

        return mendingLeft;
    };
    function getTop(obj) {
        if (obj == null)
            return null;
        var mendingObj = obj;
        var mendingTop = mendingObj.offsetTop;
        while (mendingObj != null && mendingObj.offsetParent != null && mendingObj.offsetParent.tagName != "BODY") {
            mendingTop = mendingTop + mendingObj.offsetParent.offsetTop;
            mendingObj = mendingObj.offsetParent;
        }
        return mendingTop;
    };
    //获取鼠标的位置
    function getMousePosition(event) {
        var position = {
            MouseX: 0,
            MouseY: 0
        }
        if (event.pageX != undefined) {
            position.MouseX = event.pageX;
            position.MouseY = event.pageY;
        }
        else {
            var target = EventUtil.getTarget(event);
            position.MouseX = event.offsetX + getLeft(target);
            position.MouseY = event.offsetY + getTop(target);

        }
        return position;

posted @ 2009-12-15 12:59  骑着夕阳看着猪  阅读(645)  评论(1编辑  收藏  举报