JavaScript MouseMove event

The mousemove event is very useful in web applications, but is quite tricky to use and also slows the entire page down when used in the wrong way. In this description we'll only look at the basic mousemove event, which is by far the easiest to use.

Attaching the event

The event is attached directly to a node element, document object, or in some browsers also the window object. In most cases you'll use the event on the document object, so every mouse movement on the entire page fires an event. So let's first look at a basic example:

document.onmousemove = function() {
    // Do something
}

In this example the function 'myEvent' is executed on every mouse movement on the document.

Capturing the event properties

In most cases you want to use the mouse coordinates in combination with the mousemove event. This is pretty easy to retrieve, but Internet Explorer and most other browsers use different methods for passing the event properties to the function. Firefox send the event object with the function call, while IE makes it available in a global object 'window.event'.

// Internet Explorer
document.onmousemove = function() {
    var event = window.event;
}

// Most other browsers
document.onmousemove = function(e) {
    var event = e;
}

No very handy, but its easy to make it cross-browser compatible:

document.onmousemove = function(e) {
    if (!e) e = window.event;
    // Do something
}

Capturing the mouse position

You can now retrieve the mouse coordinates from the event object, like this:

document.onmousemove = function(e) {
    if (!e) e = window.event;
    var mouseX = e.clientX, mouseY = e.clientY;
}
Replies to MouseMove event
12:44 - Saturday, 28th of May, 2011By Irish

I'm not easily impressed. . . but that's irmpesisng me! :)

14:15 - Sunday, 29th of May, 2011By wgpekg

T0lcbe <a href="http://ilcbknbiazlu.com/">ilcbknbiazlu</a>

19:04 - Sunday, 29th of May, 2011By aigbumgk

Nr0lAY , [url=http://eoqgltzaqszq.com/]eoqgltzaqszq[/url], [link=http://whrbhhwtjbyb.com/]whrbhhwtjbyb[/link], http://dhhyinvvmtje.com/

19:57 - Monday, 30th of May, 2011By Boston

More posts of this qaultiy. Not the usual c***, please

10:31 - Wednesday, 1st of June, 2011By ybxsqo

kk9Dto <a href="http://zzyvijlasiag.com/">zzyvijlasiag</a>

15:29 - Wednesday, 1st of June, 2011By bfzsxq

9oDtRo , [url=http://ofhfdqwhzscq.com/]ofhfdqwhzscq[/url], [link=http://ekzawwzqfoxt.com/]ekzawwzqfoxt[/link], http://beisxvfjdmir.com/

8:25 - Monday, 6th of June, 2011By tvhklefs

ndmcRq <a href="http://vonxuvjtpuiu.com/">vonxuvjtpuiu</a>, [url=http://hrohfspparim.com/]hrohfspparim[/url], [link=http://fuanxportjku.com/]fuanxportjku[/link], http://xmlpunrvagpp.com/

21:39 - Saturday, 30th of July, 2011By ckhopdwrd

YnCWPf <a href="http://qrrlrkpqhyku.com/">qrrlrkpqhyku</a>, [url=http://qwwqagrbgoxq.com/]qwwqagrbgoxq[/url], [link=http://emgmbrepghss.com/]emgmbrepghss[/link], http://cxhnsjlqknuo.com/

21:39 - Saturday, 30th of July, 2011By ckhopdwrd

YnCWPf <a href="http://qrrlrkpqhyku.com/">qrrlrkpqhyku</a>, [url=http://qwwqagrbgoxq.com/]qwwqagrbgoxq[/url], [link=http://emgmbrepghss.com/]emgmbrepghss[/link], http://cxhnsjlqknuo.com/

21:51 - Saturday, 12th of November, 2011By thsukykn

IUmqf5 <a href="http://zqohbmbadykr.com/">zqohbmbadykr</a>, [url=http://ujjyackpnmii.com/]ujjyackpnmii[/url], [link=http://mmfbmwbgzrkb.com/]mmfbmwbgzrkb[/link], http://dypffopppill.com/

21:51 - Saturday, 12th of November, 2011By thsukykn

IUmqf5 <a href="http://zqohbmbadykr.com/">zqohbmbadykr</a>, [url=http://ujjyackpnmii.com/]ujjyackpnmii[/url], [link=http://mmfbmwbgzrkb.com/]mmfbmwbgzrkb[/link], http://dypffopppill.com/

20:13 - Sunday, 13th of November, 2011By ubiktjyhq

kZ0d6y <a href="http://bbaqlckbmzas.com/">bbaqlckbmzas</a>, [url=http://qdxcbdwktxpi.com/]qdxcbdwktxpi[/url], [link=http://quxcphpdypkn.com/]quxcphpdypkn[/link], http://mzxduiugoxqo.com/

9:05 - Thursday, 8th of March, 2012By ozurbw

k0ZmVv <a href="http://niextfugoevk.com/">niextfugoevk</a>, [url=http://gjgmewqlkdtv.com/]gjgmewqlkdtv[/url], [link=http://hdgvrvffulgd.com/]hdgvrvffulgd[/link], http://cewwfibafmcm.com/

2:48 - Monday, 16th of April, 2012By bydsha

HLC5BV <a href="http://lybykspbjkxc.com/">lybykspbjkxc</a>, [url=http://xhipikzenyzt.com/]xhipikzenyzt[/url], [link=http://rxnvugxjaawv.com/]rxnvugxjaawv[/link], http://tshxfgknwnph.com/

Post reply

Name:
Email:
9 + 2**:
Message*:

* Required fields, ** Anti-spam question