Skip to content

Instantly share code, notes, and snippets.

@z-------------
Last active August 29, 2015 14:01
Show Gist options
  • Save z-------------/990955927bed7b3e36e9 to your computer and use it in GitHub Desktop.
Save z-------------/990955927bed7b3e36e9 to your computer and use it in GitHub Desktop.
Get mouse coordinates with mouse.coords object. The script handles the event for you.
var mouse = {};
mouse.coords = {x:-1,y:-1};
window.addEventListener("mousemove",function(e){
mouse.coords.x = e.clientX;
mouse.coords.y = e.clientY;
});
/* example:
if (mouse.coords.x > window.innerWidth / 2) {
console.log("The mouse is in the right half!")
} else {
console.log("The mouse is in the left half!")
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment