Html:
<div id= "Odiv" style= "COLOR: #666; PADDING:2PX 8px; font-size:12px; margin-right:5px; Position:absolute; Background: #fff; Display:block; border:1px solid #666; top:50px; left:10px; " > move_me</div>
The first type:
Js:
Document.onmousemove = function (ev) {var odiv= document.getelementbyid ("Odiv"); var oevent = EV | | event; Document.onmousemove = function (ev) { //ie support Event,firefox does not support var oevent = EV | | event; var scrollleft = Document.documentElement.scrollLeft | | Document.body.scrollLeft; var scrolltop = Document.documentElement.scrollTop | | Document.body.scrollTop; var navtop = Oevent.clienty + scrolltop; var navleft = Oevent.clientx + scrollleft; Odiv.style.top = navtop + ' px '; Y odiv.style.left = navleft + ' px ';//x }; } }
The second type:
Js:
Document.onmousemove = function () { var odiv= document.getelementbyid ("Odiv"); Odiv.style.top = parseint (window.event.y-40) + "px"; Odiv.style.left = parseint (window.event.x-20) + "px";}
Note the position positioning of the parent element and the position location relationship of the div that needs to be moved. My parent div Here is not set position, sub div set Position:abusolute
JS implementation of the div with the mouse to move the method