Skip to content

Instantly share code, notes, and snippets.

@hacke2
Created August 26, 2014 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hacke2/f6283dd8f01883f60aa7 to your computer and use it in GitHub Desktop.
Save hacke2/f6283dd8f01883f60aa7 to your computer and use it in GitHub Desktop.
关于光标位置的浏览器兼容方案
//获取光标相对于整个页面的当前位置(兼容各浏览器)
//获取光标的水平位置
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