Js_dom ball with Mouse move event

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.