Created
August 26, 2014 07:24
-
-
Save hacke2/f6283dd8f01883f60aa7 to your computer and use it in GitHub Desktop.
关于光标位置的浏览器兼容方案
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//获取光标相对于整个页面的当前位置(兼容各浏览器) | |
//获取光标的水平位置 | |
function getX(e){ | |
e = e || window.event; | |
//先检查非IE浏览器,在检查IE的位置 | |
return e.pageX || e.clentX + document.body.scrollLeft; | |
} | |
//获取光标的垂直位置 | |
function getY(e){ | |
e = e || window.event; | |
//先检查非IE浏览器,在检查IE的位置 | |
return e.pageY || e.clentY + document.body.scrollTop; | |
} | |
//获取鼠标相对于当前元素(事件对象e的属性target)的位置 | |
function getElementX(e){ | |
return (e && e.layerX) || window.event.offsetX; | |
} | |
function getElementY(e){ | |
return (e && e.layerY) || window.event.offsetY; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment