Call interface:
/**
* @param elem {HTMLElement} HTML element to execute animation
* @param params {JSON} HTML attributes that need to be modified during animation execution
* @param duration {Number} Optional, animation execution Time, in milliseconds
* @param easing {String} Optional, animation execution method, ease in easeIn, ease out easeOut
* @param callback {Function} Optional, callback function when animation execution is completed
* @return
*/
effect.animate(elem, params, duration, easing, callback);
Using it, you can create a switching effect of decelerating fade-out and accelerating fade-in of product images in less than 20 lines of code. Click here to view
Demonstration effect
//Auxiliary object , read/write DOM element attributes
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 = 100 * 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 the animation has been executed (how many times/frames it is actually executed)
* @param Number b: Starting position
* @param Number c: Ending position
* @param Number d: The elapsed time from the starting position to the ending position (how many times/frames are actually executed)
*/
var tween = {
//Ease in
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;
}
};
//Animation object
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; ib = 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