简体   繁体   中英

mouse cursor position in Javascript?

How do I get cursor X and Y in javascript?

var $curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
var $curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;

I had found this two, but they appear as "undefined". Any ideas?

Thanks!

I assume you don't want a framework answer. If not, try this:

document.onclick=function(evt) {
    evt = (evt || event);
    alert(evt.clientX + ' ' + evt.clientY);
}

I wasn't sure what event you wanted, so I just used an onclick event.

The accepted tutorial only link answer points to a broken link. For people coming by searching here


Check out the jquery mousemove api .

You can use the above as

$("#SomeTag").mousemove(function(event) {
  var msg = "Handler for .mousemove() called at ";
  msg += event.pageX + ", " + event.pageY;
  $("#SomeDisplayTag").append("<div>" + msg + "</div>");
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM