Home  >  Article  >  Web Front-end  >  Animate custom animation that simulates jquery implemented using js (2.5K)_jquery

Animate custom animation that simulates jquery implemented using js (2.5K)_jquery

WBOY
WBOYOriginal
2016-05-16 18:22:47951browse

Later I found out it was pretty good. Might as well keep writing.
This version is basically the same as jquery’s animate.
I mean the effect is basically the same. (The efficiency has not been tested yet.);
If there are professional testers, please help me test it.
1: Function description
Compatible with mainstream browsers.
1: Support callback function;
2: Support cascade animation call;
3: Support delay animation queue delay;
4: Support stop animation;
5: Support opacity transparency change ;
6: Support = -= *= /= operation;
7: Support unit operation (px, %);
2: Instructions for use
jelle(A).animate(B, C , D);
A: The ID of the dom element that needs to be animated;
B: The main parameters of the animation are passed {key, val, key2, val2}; such as {width:'100px',height:' =100px ',opacity:0.5},
opacity--transparency change supports = -= *= /= operations.
C: animation execution time, in milliseconds; [optional default 500 milliseconds];
D: callback function; [optional]
3: method description
1: animate() method
jelle('cc').animate({width:'100px'},300,function(){alert('Complete')});//The width of cc changes to 100px in 300 milliseconds animation The end pops up 'Complete'
2:stop() method
jelle('cc').stop();//Stop the animation being played on the cc object.
3: delay() method
jelle('cc').delay(1000).animate({width:'100px'}); //Changes in the width of cc will be executed with a delay of 1 second.
I will keep improving him.

Copy code The code is as follows:

var jelle = function(id){
var $ = function(id){ return document.getElementById(id); },
elem = $(id),//Object
f = 0, _this = {}, lazy = 10, lazyque = 10,//f animation counter lazy animation delay lazyque queue delay
//You can change the operator to make your animation different
tween = function(t, b, c, d){ return - c * (t /= d) * (t - 2) b},
// adv is used for = -= *= /= operation
adv = function(val, b){
var va, re= /^([ -\*/]=)([-]?[d.] )/ ;
if (re.test( val)){
var reg = val.match(re);
reg[2] = parseFloat(reg[2]);
switch ( reg[1] ){
case ' = ':
va = reg[2];
break;
case '-=':
va = -reg[2];
break;
case '*=' :
va = b*reg[2] - b;
break;
case '/=':
va = b/reg[2] - b;
break;
}
return va;
}
return parseFloat(val) - b;
}
// elem.animate reads the animation queue used on the current dom element
elem .animate = elem.animate || [];
//stop function to be used
jelle[id]= {};
jelle[id]['stop'] = true;
//alert(jelle[id]['stop'])
// The unified queue entry is used to conveniently set the delay, and stop
_this.entrance = function(fn, ags, lazytime){
/ /fn Call function ags parameter lazytime delay time
setTimeout(function(){
fn(ags[0], ags[1], ags[2]);
}, (lazytime || 0) );
}
// This method of stopping animation is not available yet
_this.stop = function(){
jelle[id]['stop'] = false;
elem.animate .length=0;
$(id).animate.length=0;
return _this;
}
// Queue operation
_this.queue = function(){
if (elem.animate && f == elem.animate[0].length){
f = 0; // Clear counter
elem.animate[0].callback ? elem.animate[0].callback .apply(elem) : false;
// Determine whether there is animation waiting to be executed
if (elem.animate.length > 1){
elem.animate[0].callback = elem.animate [1].callback;
elem.animate = $(id).animate || [];// Get the latest animation queue from the dom object
elem.animate.shift();// Clear the just executed Completed animation queue
$(id).animate = elem.animate;// Update the new queue to dom
var ea = elem.animate[0];
// Play the queue animation in a loop
for(var i = 0; i < ea.length; i ){
ea[i][0] === 'opacity' ? _this.entrance(_this.alpha, [ea[i][ 1], ea[i][2]], lazyque):
_this.entrance(_this.execution, [ea[i][0], ea[i][1], ea[i][2] ], lazyque);
}
}else{
elem.animate.length = 0; // The queue is clear
$(id).animate.length = 0; // The queue is clear
}
}
}
//Set lazy method, future queue animation delay time
_this.delay = function(val){
lazyque = val;
return _this;
}
//Animation changes
_this.execution = function(key, val, t){
//alert(val)
var s = (new Date()).getTime (), d=t || 500 ,
b = parseFloat(elem.style[key]) || 0 ,
c = adv(val, b) ,// adv is used to set advanced operations such as = -= etc.
un = val.match(/d (. )/)[1];// unit
(function(){
var t = (new Date()).getTime( ) - s;
if (t > d){
t = d;
elem.style[key] = parseInt(tween(t, b, c, d)) un;
_this.queue(); // Operation queue
return _this;
}
elem.style[key] = parseInt(tween(t, b, c, d)) un;
jelle[ id]['stop'] && setTimeout(arguments.callee, lazy);
// _this.entrance(arguments.callee,[1,1,1],lazy);
// arguments.callee anonymous Function recursive call
})();
}
// Entry
_this.animate = function(sty, t, fn){
// sty, t, fn are changes respectively Parameter key, val form, animation time, callback function
var len = elem.animate.length; // len checks the animation queue length
elem.animate[len] = [];
elem.animate [len].callback = fn;
//Multi-key loop setting changes
for(var i in sty){
elem.animate[len].push([i, sty[i], t ]);
if(len == 0){
i == 'opacity' ? _this.entrance(_this.alpha, [sty[i], t], lazyque) :
_this.entrance (_this.execution, [i, sty[i], t], lazyque);
}
}
$(id).animate = elem.animate;//Add a new animation queue to
return _this on the dom element;
}
// Code for transparency change
_this.alpha = function(val, t){
var s = (new Date()).getTime (),
d = t || 500, b, c;
if( document.defaultView ){
b = document.defaultView.getComputedStyle(elem,null)['opacity'] || 1 ,
c = adv(val,b) * 100;
(function(){
var t = (new Date()).getTime() - s;
if(t > d){
t = d;
elem.style['opacity'] = tween(t, (100 * b), c, d) / 100;
_this.queue(); // Queue control
return _this;
}
elem.style['opacity'] = tween(t, (100 * b), c, d) / 100;
jelle[id][' stop'] && setTimeout(arguments.callee, lazy);
})()
}else{
b = elem.currentStyle['filter'] ?
(elem.currentStyle['filter '].match(/^alpha(opacity=([d.] ))$/))[1]/100 : 1;
c = adv(val, b) * 100;
(function( ){
var t = (new Date()).getTime() - s;
if (t > d){
t = d;
elem.style['filter'] ='alpha(opacity=' tween(t, (100 * b), c, d) ')';
_this.queue(); // Queue control
return _this;
}
elem.style['filter'] = 'alpha(opacity=' tween(t, (100*b) , c, d) ')';
jelle[id]['stop'] && setTimeout (arguments.callee, lazy);
})()
}
}
return _this;
}


Code packaging Download

programs may be modified every day. If you want the latest ainimate, you can contact me by email.

The above code is no longer up to date.

Several errors have been corrected in the past two days.
This article comes from Blog Park jelle blog http://www.cnblogs.com/idche/
Statement:
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