Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get mouse position within div? [duplicate]

Tags:

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; 
like image 490
Max Hudson Avatar asked Feb 01 '13 16:02

Max Hudson


People also ask

How do I find the cursor position in an element?

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.

How to get mouse position in React?

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.

What is mousemove event?

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 change the mouse position in Python?

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.


1 Answers

Using jQuery:

var divPos = {}; var offset = $("#divid").offset(); $(document).mousemove(function(e){     divPos = {         left: e.pageX - offset.left,         top: e.pageY - offset.top     }; }); 
like image 57
PitaJ Avatar answered Sep 23 '22 20:09

PitaJ