首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > CSS >

JS 鼠标相对document的座标以及HTML元素相对document的坐标

2012-10-29 
JS 鼠标相对document的坐标以及HTML元素相对document的坐标Number.prototype.NaN0 function(){return is

JS 鼠标相对document的坐标以及HTML元素相对document的坐标

Number.prototype.NaN0 = function(){return isNaN(this)?0:this;//如果this为非数字返回0,否则返回数字}//获取鼠标相对document的坐标function getMouseCoord(evt){if(evt.pageX && evt.pageY)return {x:evt.pageX,y:pageY};return {x:evt.clientX + document.body.scrollLeft - document.body.clientLeft,y:evt.clientY + document.body.scrollTop - document.body.clientTop};}//获取HTML元素相对document的坐标function getPosition(ele){var x = ele.offsetLeft+(ele.curentStyle?(parseInt(ele.curentStyle.borderLeftWidth).NaN0()):0);var y = ele.offsetTop+(ele.curentStyle?(parentInt(ele.curentStyle.borderTopWidth).NaN0()):0);while(ele.offsetParent){ele = ele.offsetParent;x+=ele.offsetLeft+(ele.curentStyle?(parseInt(ele.curentStyle.borderLeftWidth).NaN0()):0);y+=ele.offsetTop+(ele.curentStyle?(parentInt(ele.curentStyle.borderTopWidth).NaN0()):0);}return {x:x,y:y};}//获取鼠标相对与点击空件上的坐标function getMouseOffset(target,ev){var elePosition = getPosition(target);var mouseCoord = getMouseCoord(ev);return {x:mouseCoord.x-elePosition.x,y:mouseCoord.y-elePosition.y};}
? 1 楼 jidifeiyan 2010-11-16   今天也碰到了这个问题,看了这篇文章解决了,不错。

热点排行