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
JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!