Personal site
The movement of a div is actually a change in the distance between it and the browser border. If the change rate is certain, it isConstant SpeedIf the change rate is not certain, it is a variable speed movement. When the change rate is proportional to the distance from the browser's border, it can be said that DIV is doingBuffer Motion.
In fact, it is very simple to use a timer to change the border distance of the DIV aggregation browser at intervals.
For example, uniform motion:
Enter the Timer: (every 30 ms)
If (whether to reach the end point)
{Stop timer}
Else do {Change distance}
The method of changing the distance determines whether the constant speed or variable speed (buffer) motion.
For example:
Javascript Code
- VaRTimer =Null;
-
-
- FunctionStartmove ()
-
- {
-
-
- VaROdiv = Document. getelementbyid ('Div1');
-
- Clearinterval (timer );
-
- Timer = setinterval (Function(){
-
- VaRIspeed = 1;
-
-
- If(Odiv. offsetleft> = 300)
- {
-
- Clearinterval (timer );
-
- }
-
- Else
-
- {
-
- Odiv. style. Left = odiv. offsetleft + ispeed +'Px';
-
- }
-
- }, 30 );
-
- };
Buffer movement:
Java code
-
- VaR timer =Null;
- Function startmove ()
-
- {
-
- VaR itarget =300;
-
-
- VaR odiv = Document. getelementbyid ('Div1');
-
-
- Clearinterval (timer );
-
- Timer = setinterval (function (){
-
- VaR ispeed = (iTarget-oDiv.offsetLeft )/8;
-
-
- Ispeed = ispeed>0? Math. Ceil (ispeed): Math. Floor (ispeed );
-
- Ispeed = math. Ceil (ispeed );
-
- If(Odiv. offsetleft = itarget)
-
- {
-
- Clearinterval (timer );
-
- }
-
- Else
-
- {
-
- Odiv. style. Left = odiv. offsetleft + ispeed +'Px';
-
- }
-
- Document. Title = odiv. style. Left +''+ Ispeed;
-
- },30);
-
- };
In this way, a motion frame is ready! The principle is very simple, but it still needs to be improved. Come on!