Use queue to simulate jquery animation algorithm example_jquery
The example in this article describes the animation algorithm of using queue to simulate jquery. Share it with everyone for your reference. The specific analysis is as follows:
Aaron has recently fallen madly in love with algorithm research, which is probably going to cost a lot of brain cells. I like to pick up ready-made ones to save some effort. I found a piece of source code he wrote and it was quite fun to run it, so I used it to analyze it, firstly to absorb the nutrients inside, and secondly to deepen my skills in source code learning. It is said that this source code is really a secret to improving the internal strength of js. If you don’t believe it, come and taste it with me.
/**
(function($){
//The $ here will be provided by the return value of the immediate execution function that follows
})(function(){
//The result of running this function is $
Return aQuery
}())
*/
(function($) {
window.$ = $;
})(function() {
//Used to match ID string
//(?: Indicates no grouping here), refer to the regular content
//But I personally think it would be better to change * to a sign, because there must be at least one character after #
var rquickExpr = /^(?:#([w-]*))$/;
//At first glance, he is a severe patient of jquery
Function aQuery(selector) {
return new aQuery.fn.init(selector);
}
/**
* Animation
* @return {[type]} [description]
*/
var animation = function() {
var self = {};
var Queue = []; //Animation Queue
var firing = false //Animation lock
var first = true; //Triggered through add interface
var getStyle = function(obj, attr) {
return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj, false)[attr];
}
//These are all specific animation effects, there is nothing difficult to understand
var makeAnim = function(element, options, func) {
var width = options.width
// Packaged a specific execution algorithm
//css3
//setTimeout
element.style.webkitTransitionDuration = '2000ms';
element.style.webkitTransform = 'translate3d(' width 'px,0,0)';
//Monitoring animation completed
element.addEventListener('webkitTransitionEnd', function() {
func()
});
}
var _fire = function() {
//The added animation is being triggered
if (!fireing) {
var onceRun = Queue.shift();
if (onceRun) {
//Prevent repeated triggering
firing = true;
//next
onceRun(function() {
fire = false;
//The effect of serial calls is very cleverly produced here
_fire();
});
} else {
firing = true;
}
}
}
return self = {
//Add queue
add: function(element, options) {
//Here is the key to the entire algorithm
//Equivalent to adding a function to the array
//[function(func){},...]
// That is the onceRun method in _fire, and func was passed in at that time.
// Aaron likes to use this technique in his programming, such as pre-compilation and so on.
Queue.push(function(func) {
makeAnim(element, options, func);
});
//If there is a queue, trigger the animation immediately
If (first && Queue.length) {
//This switch plays a very good role in controlling the queuing of elements added later
1st = false;
//This is equivalent to running _fire();
directly
// Aaron likes to install A, deliberately add a self.fire, maybe he is far -sighted
self.fire();
}
},
//Trigger
Fire: function() {
_fire();
}
}
}();
aQuery.fn = aQuery.prototype = {
run: function(options) {
animation.add(this.element, options);
return this;
}
}
var init = aQuery.fn.init = function(selector) {
var match = rquickExpr.exec(selector);
var element = document.getElementById(match[1])
This.element = element;
return this;
}
//I almost underestimated this line of code
//I learned how to use jquery well
//Isn’t it better to directly aQuery.fn.init = aQuery.fn?
//One more init variable is just to reduce queries, the idea of optimization is everywhere.
init.prototype = aQuery.fn;
Return aQuery;
}());
//dom
var oDiv = document.getElementById('div1');
//Call
oDiv.onclick = function() {
$('#div1').run({
'width': '500'
}).run({
'width': '300'
}).run({
'width': '1000'
});
};
Attach the html and you can adjust it yourself. Remember to use chrome to browse.
I hope this article will be helpful to everyone’s jQuery programming.

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 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 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.

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.

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.

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.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft