search
HomeWeb Front-endJS TutorialAnimate custom animation that simulates jquery implemented using js (2.5K)_jquery

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[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
jquery实现多少秒后隐藏图片jquery实现多少秒后隐藏图片Apr 20, 2022 pm 05:33 PM

实现方法:1、用“$("img").delay(毫秒数).fadeOut()”语句,delay()设置延迟秒数;2、用“setTimeout(function(){ $("img").hide(); },毫秒值);”语句,通过定时器来延迟。

jquery怎么修改min-height样式jquery怎么修改min-height样式Apr 20, 2022 pm 12:19 PM

修改方法:1、用css()设置新样式,语法“$(元素).css("min-height","新值")”;2、用attr(),通过设置style属性来添加新样式,语法“$(元素).attr("style","min-height:新值")”。

axios与jquery的区别是什么axios与jquery的区别是什么Apr 20, 2022 pm 06:18 PM

区别:1、axios是一个异步请求框架,用于封装底层的XMLHttpRequest,而jquery是一个JavaScript库,只是顺便封装了dom操作;2、axios是基于承诺对象的,可以用承诺对象中的方法,而jquery不基于承诺对象。

jquery怎么在body中增加元素jquery怎么在body中增加元素Apr 22, 2022 am 11:13 AM

增加元素的方法:1、用append(),语法“$("body").append(新元素)”,可向body内部的末尾处增加元素;2、用prepend(),语法“$("body").prepend(新元素)”,可向body内部的开始处增加元素。

jquery中apply()方法怎么用jquery中apply()方法怎么用Apr 24, 2022 pm 05:35 PM

在jquery中,apply()方法用于改变this指向,使用另一个对象替换当前对象,是应用某一对象的一个方法,语法为“apply(thisobj,[argarray])”;参数argarray表示的是以数组的形式进行传递。

jquery怎么删除div内所有子元素jquery怎么删除div内所有子元素Apr 21, 2022 pm 07:08 PM

删除方法:1、用empty(),语法“$("div").empty();”,可删除所有子节点和内容;2、用children()和remove(),语法“$("div").children().remove();”,只删除子元素,不删除内容。

jquery怎么去掉只读属性jquery怎么去掉只读属性Apr 20, 2022 pm 07:55 PM

去掉方法:1、用“$(selector).removeAttr("readonly")”语句删除readonly属性;2、用“$(selector).attr("readonly",false)”将readonly属性的值设置为false。

jquery on()有几个参数jquery on()有几个参数Apr 21, 2022 am 11:29 AM

on()方法有4个参数:1、第一个参数不可省略,规定要从被选元素添加的一个或多个事件或命名空间;2、第二个参数可省略,规定元素的事件处理程序;3、第三个参数可省略,规定传递到函数的额外数据;4、第四个参数可省略,规定当事件发生时运行的函数。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!