JavaScript animation object Support acceleration, deceleration, ease, ease out of the implementation of code _JAVASCRIPT skills

Source: Internet
Author: User
Call Interface:
Copy Code code as follows:

/**
* @param elem {htmlelement} HTML element that performs animation
* @param params {JSON} The HTML attributes that need to be modified during the execution of the animation
* @param duration {Number} optional, animation execution time, unit milliseconds
* @param easing {String} optional, the way the animation executes, slowly enters Easein, eases out easeout
* @param callback {function} optional, callback function at completion of animation execution
* @return
*/
Effect.animate (Elem, params, duration, easing, callback);

Use it in 20 lines of code can do a product picture deceleration fade out, accelerated fade in transition effect Click here to see the demo effect
Copy Code code as follows:

Secondary object, read/write DOM element properties
var attribute = {
Get:function (Elem, attr) {
var Val;
if (Elem.currentstyle) {
if (attr = = "opacity") {
val = elem.filters.alpha[attr];
}else {
val = elem.currentstyle[attr];
}
}
else{
val = getComputedStyle (Elem) [attr];
if (attr = = "opacity") {
val = * Val;
}
}
return Val;
},
Set:function (Elem, attr, Val) {
if (attr== ' opacity ') {
Elem.style.filter = ' alpha (opacity= ' + (val) + ') ';
Elem.style.opacity = (val)/100;
}
else{
Elem.style[attr] = val + ' px ';
}
}
};
/*
* Description: Tween animation algorithm.
* @param number T: The time that the animation has been performed (how many times/frames actually performed)
* @param number B: Starting position
* @param number C: Termination position
* @param number D: Elapsed time from the start position to the end position (how many times/frames were actually performed)
*/
var tween = {
Ease into
Easein:function (T, B, C, D) {
Return c * (t/=d) * t + B;
},
Ease out
Easeout:function (t,b,c,d) {
Return-c * (t/=d) * (t-2) + B;
}
};
Animated objects
var effect = {
Animate:function (Elem, params, duration, easing, callback) {
var dt = new Date (). GetTime (),
b = 0,
c = 0,
d = Duration | | 500,
fps = 1000/60;
var changes = [];
for (var attr in params) {
b = parsefloat (Attribute.get (Elem, attr));
c = params[attr]-B;
Changes.push ({
Attr:attr,
B:B,
C:c
});
}
Easing = Easing | | "Easeout";
Callback = Callback | | New Function;
settimeout (function () {
var t = new Date (). GetTime ()-DT;
var b, C, attr;
for (var i=0; i<changes.length; i++) {
b = changes[i].b;
c = changes[i].c;
attr = changes[i].attr;
Attribute.set (Elem, attr, tween[easing] (t, B, C, D));
if (d <= t) {
Attribute.set (Elem, attr, params[attr]);
Callback ();
Return
}
}
SetTimeout (Arguments.callee, fps);
, FPS);
}
};
by rentj1@163.com

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.