Skip to content

Instantly share code, notes, and snippets.

@hehongwei44
Created July 27, 2014 09:07
Show Gist options
  • Save hehongwei44/2732365bd42baa491ef8 to your computer and use it in GitHub Desktop.
Save hehongwei44/2732365bd42baa491ef8 to your computer and use it in GitHub Desktop.
获取鼠标位置的几个通用的函数
/*两个通用函数,用于获取鼠标相对于整个页面的当前位置*/
function getX(e) {
e = e || window.event;
return e.pageX || e.clientX + document.body.scrollLeft;
}
function getY(e) {
e = e || window.event;
return e.pageY || e.clientY + document.body.scrollTop;
}
/*两个获取鼠标相对于当前元素位置的函数*/
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