【jQuery】获取鼠标位置

//获取鼠标距dom0,0坐标 与当前对象0,0坐标的位置
    .factory("getPosition",function(){
        return{
            // 获取当前dom元素
            getCrossBrowserElement:function(mouseEvent){
                var result = {
                    x: 0,
                    y: 0,
                    relativeX: 0,
                    relativeY: 0,
                    currentDomId: ""
                };

                if (!mouseEvent){
                    mouseEvent = window.event;
                }

                if (mouseEvent.pageX || mouseEvent.pageY){
                    result.x = mouseEvent.pageX;
                    result.y = mouseEvent.pageY;
                }
                else if (mouseEvent.clientX || mouseEvent.clientY){
                    result.x = mouseEvent.clientX + document.body.scrollLeft +
                        document.documentElement.scrollLeft;
                    result.y = mouseEvent.clientY + document.body.scrollTop +
                        document.documentElement.scrollTop;
                }

                result.relativeX = result.x;
                result.relativeY = result.y;

                if (mouseEvent.target){
                    var offEl = mouseEvent.target;
                    var offX = 0;
                    var offY = 0;
                    if (typeof(offEl.offsetParent) != "undefined"){
                        while (offEl){
                            offX += offEl.offsetLeft;
                            offY += offEl.offsetTop;
                            offEl = offEl.offsetParent;
                        }
                    }
                    else{
                        offX = offEl.x;
                        offY = offEl.y;
                    }

                    result.relativeX -= offX;
                    result.relativeY -= offY;
                }
                result.currentDomId = mouseEvent.target.id;

                return result;
            }
        }
    })

你可能感兴趣的:(JQuery)