Dom Event Mouse Event-Small case (block with mouse move event)
Html
1 < ID= "ballimg" src= "./images/ball-light-2.gif" title= " Color flashing Ball ">
Js:
1 varCurrentX = 0;2 varCurrentY = 0;3 varDesX = 0;4 varDesY = 0; 5 6Window.onload =function(){7 varBall = document.getElementById ("ballimg");8 9 Ten OneDocument.addeventlistener ("MouseMove",function(event) { A varE = event| |window.event; - -DesX = E.CLIENTX-BALL.OFFSETWIDTH/2; theDesY = E.CLIENTY-BALL.OFFSETHEIGHT/2; - //Console.log (desx+ "," +dexy); - move (ball); - }) + - + } A varTimer =NULL; at functionMove (ball) { - clearinterval (timer); - vari = 0; - varSpeedx = (desx-currentx)/100; - varSpeedY = (desy-currenty)/100; -Timer = SetInterval (function(){ in //Console.log (desx+ "," +dexy); -CurrentX = CurrentX +Speedx toCurrentY = CurrentY +SpeedY +Ball.style.left = currentx+ "px"; -Ball.style.top = currenty + "px"; thei++; * $ Console.log (i)Panax Notoginseng if(i = = 100){ -Ball.style.left = desx+ "px"; theBall.style.top = DesY + "px"; + clearinterval (timer); A } the},1) +}
Effect display: (PS lazy Add the boundary of haha haha ha ha haha just make a look? )
The above content, if there are errors please point out, not very grateful.
Js_dom ball with Mouse move event