Home > Web Front-end > JS Tutorial > body text

JavaScript animation objects support acceleration, deceleration, ease-in, and ease-out implementation code_javascript skills

WBOY
Release: 2016-05-16 17:49:33
Original
1602 people have browsed it

Call interface:

Copy code The code is as follows:

/**
* @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
Copy code The code is as follows:

//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
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template