I want to change the left and top style element with javascript on an onmousemove event, but it does not work.
My code for the onmousemove event:
function mymove(e)
{
var event=new MouseEvent(e);
x = 20;
y = 10;
document.getElementById('mye').style.left = event.pageX + x+'px' ;
document.getElementById('mye').style.top = event.pageY - y+'px';
}
Guessing on the markup and CSS. Also, I think the
e
properties might change per browser. This works in Firefox (9).CSS
Markup
Javascript
http://jsfiddle.net/3YdEa/6/
And note, as Jeffrey Sweeney mentions, attaching to the
window.onmousemove
is probably more common:http://jsfiddle.net/3YdEa/7/
Here is Quirksmode on the mouse event position property situation. This is a few years old, however.
Here's another StackOverflow question, and of course jQuery's
$.mousemove()
, which will eliminate the differences between browsers.