How to Find the Coordinates of Your Mouse Position
Did you know you can use jQuery to find the coordinates of your cursor? It only takes a few lines of code to get the x and y coordinates of where the mouse is positioned on a page. With these coordinates, you can execute other functions and lines of code based on where the cursor is located on the page. It's also just a cool trick to offer your users -- for example, you can choose to allow them to click a button that will reveal the x and y coordinates of their mouse.
Check out the code snippet below to see how it's done:
function cursorPosition(elementID, mouseX, mouseY) {
var offset = $('#'+elementID).offset();
var x = mouseX - offset.left;
var y = mouseY - offset.top;
return {'x': x, 'y': y};
}
Insert the above function into your code to reveal the values of the x and y coordinates of your cursor's position. This code can be customized to be used however you like it -- if you want to execute a function based on the cursor's position, you'll probably need an if statement. If you want to reveal the cursor's coordinates to your users, you might want a click event (or some sort of trigger event) and code that will make the data available to the user (perhaps an alert box). The possibilities are endless.