Javascript Swing Motion (reciprocating motion of the same horizontal line)

Source: Internet
Author: User
A swinging motion means that an object is reciprocating around a certain axis within a certain angle. Now we use js to simulate it. The implementation principle is to use setInterval or setTimeout to dynamically change the distance between an element and an axis. First, the element is located near the axis at a certain position (with a speed of 0 ).

A swinging motion means that an object is reciprocating around a certain axis within a certain angle. Now we use js simulation. The implementation principle is to use setInterval or setTimeout to dynamically change the distance between an element and an axis. First, the acceleration motion close to the axis is performed from a certain position (the speed is 0, when the axis is reached (the speed reaches the maximum value), then the speed reduction movement away from the axis will be done. When the distance reaches a certain distance (the speed is 0), the reverse movement will be done, in this way, the swinging motion is formed.

Html section:

The element with id btn1 is a button used to trigger motion. The element with id div1 is the object to be motion, and the element with class = line is the axis.

123 GO

Css section:

123 * {Margin: 0; padding: 0 ;}. div1 {width: 100px; height: 100px; background: #f60url(qiuweiguan.gif) no-repeatcentercenter ;}. line {position: relative; left: 300px; top:-100px; width: 1px; height: 500px; background: #666 ;}

Js section:

The setInterval method is used to implement motion, and then the speed is changed every time this method is executed, so that uniform acceleration or even deceleration can be performed. Then, let's make another judgment to determine when to perform acceleration and when to perform deceleration.

1234567891011121314151617181920212223
Related Article

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.