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.
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 |
|