A very small JavaScript library that allows you to track a client's cursor position on an element
You can download Mouse.js and add it to your HTML:
<script src="scripts/Mouse.js"></script>Next, initialize it with a line of JavaScript:
var mouse = new MouseJS(someElement);You can find
- Where the user's mouse position with
mouse.x``` and ```mouse.y - Where the user last left clicked with
mouse.left.before.x - When the user last pressed down their left mouse button with
mouse.left.clickTime - When the user last released their left mouse button with
mouse.left.releaseTime - And the same for the right mouse button
All MouseJS times are formatted in milliseconds since the object's initialization. For example:
console.log(mouse.right.clickTime); // might be something like 1250 (for 1.25 seconds)You can tell if the client's left mouse button is down with:
console.log(mouse.left.releaseTime < mouse.getTime())Note that the HTML element has to have focus for MouseJS to be able to track the client's mouse. You can force an element to have focus with
someElement.focus();