Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
5
rated 0 times [  10] [ 5]  / answers: 1 / hits: 19327  / 12 Years ago, sun, july 1, 2012, 12:00:00

How can I continuously get the position of the mouse whilst its button is being held down?



I know I can do:



<element onclick=functionName(event)></element>
<script>
function functionName(e){
e.pageX
e.pageY
//do stuff
}
</script>


and I know you can use the onmousedown event, but how would I continuously get the position while the button is still held down?



Oddly I couldn't find this anywhere I looked.


More From » jquery

 Answers
125

Anyway, I'd suggest using mousemove event with check if which event property equals 1 (i.e. left mouse button pressed):



$(element).on(mousemove, function(e) {
if (e.which == 1) {
console.log(e.pageX + / + e.pageY);
}
});​


DEMO: http://jsfiddle.net/HBZBQ/


[#84544] Friday, June 29, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daja

Total Points: 407
Total Questions: 103
Total Answers: 103

Location: Ghana
Member since Sun, Mar 27, 2022
2 Years ago
daja questions
Tue, Dec 21, 21, 00:00, 2 Years ago
Thu, Apr 23, 20, 00:00, 4 Years ago
Fri, Sep 6, 19, 00:00, 5 Years ago
Tue, Jul 23, 19, 00:00, 5 Years ago
;