Possible Duplicate:
Mouse position relative to div
getting mouse position with javascript within canvas
How can I get the position of the mouse within a canvas that is a fixed size but has an automatic margin?
I can't make its position fixed and can't just use the regular mouse position on the page.
This code works perfectly:
mouseX = e.pageX - div.offsetLeft; mouseY = e.pageY - div.offsetTop;
You can use the jQuery event. pageX and event. pageY in combination with the jQuery offset() method to get the position of mouse pointer relative to an element.
To get the position of the mouse in React, add a mousemove event handler to the window object and access the clientX and clientY properties of the MouseEvent object to get the X and Y coordinates of the mouse respectively. The current mouse position is displayed.
The mousemove event is fired at an element when a pointing device (usually a mouse) is moved while the cursor's hotspot is inside it.
How do I get the mouse position in Python using Pyautogui? To determine the mouse's current position, we use the statement, pyautogui. position(). This function returns a tuple of the position of the mouse's cursor.
Using jQuery:
var divPos = {}; var offset = $("#divid").offset(); $(document).mousemove(function(e){ divPos = { left: e.pageX - offset.left, top: e.pageY - offset.top }; });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With