JS Spring Effect code

Source: Internet
Author: User

Although the spring effect is used, acceleration and deceleration between fixed-point coordinates must be realized.
Point-to-point movement should know how to do it. This is achieved by setting the left of the moving object.
The deceleration effect is generally obtained by dividing the current value by a coefficient (generally a positive integer) by the target value.
Then the current value is added with this step as the new current value, and the value is repeated until the current value is equal to the target value.
As the step size is getting smaller and smaller, and the step size is the moving value, the deceleration effect is achieved.
How can we achieve acceleration?
I thought of a method because I couldn't get the acceleration step corresponding to the deceleration step (or I couldn't think of a method,
First, calculate all the deceleration step values and put them in an array. As the deceleration step, the acceleration step is the reverse of the array (that is, the reverse ).
This part is mainly used in the setstep () function. You can refer Code .
Other parts are described in the Code.

ProgramCode:
CodeCopy codeThe Code is as follows: var $ = function (ID ){
Return "string" = typeof ID? Document. getelementbyid (ID): ID;
};

Function addeventhandler (otarget, seventtype, fnhandler ){
If (otarget. addeventlistener ){
Otarget. addeventlistener (seventtype, fnhandler, false );
} Else if (otarget. attachevent ){
Otarget. attachevent ("On" + seventtype, fnhandler );
} Else {
Otarget ["on" + seventtype] = fnhandler;
}
};

VaR class = {
Create: function (){
Return function (){
This. Initialize. Apply (this, arguments );
}
}
}

Object. Extend = function (destination, source ){
For (VAR property in source ){
Destination [property] = source [property];
}
Return destination;
}

VaR bounce = Class. Create ();
Bounce. Prototype = {
// Container object, sliding object, original position, and moving range
Initialize: function (container, OBJ, iorigin, irange, options ){

This. _ OBJ = $ (OBJ); // sliding object
This. _ XO = parseint (iorigin); // coordinate of the central axis (that is, the original coordinate)
This. _ XT = 0; // target coordinate
This. _ xs = []; // target coordinate set
This. _ steps = []; // step set
This. _ fast = true; // whether to accelerate

This. range = irange | 0; // sliding range (width)

This. setoptions (options );

This. Step = parseint (this. Options. Step );
This. Time = parseint (this. Options. time );
This. Zoom = parseint (this. Options. Zoom );
This. Reduce = !! This. Options. Reduce;
This. min = parseint (this. Options. min );
This. max = parseint (this. Options. max );
This. onmin = This. Options. onmin;
This. onmax = This. Options. onmax;
This. onside = This. Options. onside;

// Set the style
$ (Container). style. Position = "relative ";
This. _ obj. style. Position = "absolute ";
This. _ obj. style. Left = This. _ XO + "PX ";

If (this. Range> 0) This. Start ();
},
// Set the default attribute
Setoptions: function (options ){
This. Options = {// Default Value
Step: 10, // sliding Change Rate
Time: 10, // sliding Delay
Zoom: 0, // scaling Change Rate
Reduce: True, // whether to zoom out
Min: 0, // minimum range
MAX: 0, // maximum range
Onmin: function () {}, // The minimum execution time
Onmax: function () {}, // execute when the maximum value is reached
Onside: function () {}// executed when the boundary is reached
};
Object. Extend (this. Options, options || {});
},
// Start from the axis
Start: function (irange ){
Cleartimeout (this. _ timer );
// If irange has a value, reset the sliding range.
If (irange) This. range = irange;
// Determines whether the vertex has reached the minimum point
If (this. Reduce & (this. range <= 0 | this. range <= This. min) {This. onmin (); return ;}
// Determines whether the maximum vertex has been reached
If (! This. Reduce & (this. max> 0 & this. Range> = This. max) {This. onmax (); return ;}
// Reset the position
This. _ obj. style. Left = This. _ XO + "PX ";
// Set the target coordinate set (irange may change, so it must be set every time)
This. _ xs = [This. _ XO + this. Range, this. _ XO, this. _ xo-This. Range, this. _ XO];
// Set to acceleration
This. _ fast = false;
// Start segmented Movement
This. Set ();
},
// Start from the segmentation
Set: function (){
// Return after the target coordinates are reached
If (this. _ Xs. Length <= 0 ){
// Reset the range if the scaling change rate has a value
If (this. Zoom> 0) {This. range + = (this. Reduce? -1: 1) * This. Zoom ;}
This. Start (); return;
}
// Obtain the target coordinate
This. _ XT = This. _ Xs. Shift ();
// The target coordinate is the central axis, indicating that the target coordinate is on the boundary.
If (this. _ XT = This. _ XO) This. onside ();
// Set the step size
This. setstep ();
// Start to move
This. Move ();
},
// Move
Move: function (){
Cleartimeout (this. _ timer );
// After the step is completed, the result is returned when the target coordinate is reached.
If (this. _ steps. Length <= 0) {This. Set (); return ;}
// Execute mobile
This. _ obj. style. Left = (parseint (this. _ obj. style. Left) + this. _ steps. Shift () + "PX ";
// Move cyclically
VaR othis = this; this. _ timer = setTimeout (function () {othis. Move () ;}, this. Time );
},
// Set the step size
Setstep: function (){
VaR itemp = parseint (this. _ obj. style. Left );

// Note that it is from large to small
This. _ steps = [];

If (this. Step> = 1 ){
VaR I = 0;
Do {
I = (this. _ XT-itemp)/This. step;
// The step size cannot contain 0
If (I = 0) {break;} else if (math. Abs (I) <1) {I = I> 0? 1:-1 ;}
This. _ steps. Push (I = parseint (I ));
Itemp + = I;
} While (true );
// If acceleration is enabled, reverse the step set.
If (this. _ fast) This. _ steps. Reverse ();
}
// The acceleration and deceleration Are alternating, so we need to reverse each time.
This. _ fast =! This. _ fast;
}
};

Test HTML: Copy code The Code is as follows: <style type = "text/CSS">
. Container {border: 1px solid #000000; Height: 50px; width: 500px ;}
. Bounce {width: 10px; Height: 10px; Background: #000000; top: 20px ;}
</Style>
Fixed Range rebound:
<Div id = "idcontainer" class = "Container">
<Div id = "idbounce" class = "Bounce"> </div>
</Div>
<Br/>
Range gradient rebound:
<Div id = "idcontainer1" class = "Container">
<Div id = "idbounce1" class = "Bounce"> </div>
</Div>
<Br/>
Custom range rebound:
<Div id = "idcontainer2" class = "Container">
<Div id = "idbounce2" class = "Bounce"> </div>
</Div>
<Br/>
Range:
<Input id = "AA" name = "" type = "text" value = "200" size = "8"/>
<Input id = "BB" name = "" type = "button" value = "start"/>
<Input id = "idfast" name = "" type = "button" value = "acceleration +"/>
<Input id = "idslow" name = "" type = "button" value = "slowdown-"/>
<Input id = "idzoom" name = "" type = "button" value = ""/>

Test code:Copy codeCode: New bounce ("idcontainer", "idbounce", 250,200 );
VaR o = New bounce ("idcontainer1", "idbounce1", 250,200 ,{
Zoom: 20, Max: 200,
Onmax: function () {o. Reduce = true; O. Start (200 );},
Onmin: function () {o. Reduce = false; O. Start (0 );}
});

VaR O2 = New bounce ("idcontainer2", "idbounce2", 250 );
$ ("BB"). onclick = function () {o2.start (parseint ($ ("AA"). Value) || 200 );}
$ ("Idfast"). onclick = function () {If (-- o2.step <2) {o2.step = 2 }}
$ ("Idslow"). onclick = function () {If (++ o2.step> 20) {o2.step = 20 }}
$ ("Idzoom"). onclick = function () {o2.zoom = 50 ;}

Xmlns = "http://www.w3.org/1999/xhtml">


Fixed Range rebound:

Range gradient rebound:

Custom range rebound:

range:

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.