Using JavaScript to implement animation, the principle is very simple: we only need to modify the CSS style of the DOM element a little at a fixed time interval (for example, 0.1 seconds) each time (for example, increase the height and width by 10%). It looks like It's like animation.
But to manually implement animation effects using JavaScript requires writing very complex code. If you want to encapsulate animation effects with functions for easy reuse, there are more things to consider. This article mainly shares with you several animation styles built into jQuery.
Using jQuery to implement animation, the code cannot be simpler: only one line of code is needed!
Let us first take a look at several jQuery built-in animation styles:
show / hide
Call show() and hide( directly without parameters ), will show and hide DOM elements. However, as long as a time parameter is passed in, it becomes an animation:
var p = $('#test-show-hide'); p.hide(3000); // 在3秒钟内逐渐消失
The time is in milliseconds, but it can also be strings such as 'slow' and 'fast':
var p = $('#test-show-hide'); p.show('slow'); // 在0.6秒钟内逐渐显示
The toggle() method determines whether to show() or hide() based on the current status.
slideUp / slideDown
You may have seen that show() and hide() gradually expand or shrink from the upper left corner, while slideUp() and slideDown() Gradually expand or contract in the vertical direction.
slideUp() closes a visible DOM element, the effect is like closing a curtain, slideDown() is the opposite, and slideToggle() determines the next action based on whether the element is visible:
var p = $('#test-slide'); p.slideUp(3000); // 在3秒钟内逐渐向上消失
fadeIn / fadeOut
The animation effect of fadeIn() and fadeOut() is to fade in and out, which is achieved by continuously setting the opacity attribute of the DOM element, while fadeToggle() is determined based on whether the element is visible. Next step:
var p = $('#test-fade'); p.fadeOut('slow'); // 在0.6秒内淡出
Custom animation
If the above animation effects cannot meet your requirements, then use the last resort: animate(), which can achieve any animation effect. The parameters we need to pass in are the final CSS status and time of the DOM element. jQuery continuously adjusts the CSS within the time period until it reaches the value we set:
var p = $('#test-animate'); p.animate({ opacity: 0.25, width: '256px', height: '256px' }, 3000); // 在3秒钟内CSS过渡到设定值
animate() can also pass in a function, When the animation ends, this function will be called:
var p = $('#test-animate'); p.animate({ opacity: 0.25, width: '256px', height: '256px' }, 3000, function () { console.log('动画已结束'); // 恢复至初始状态: $(this).css('opacity', '1.0').css('width', '128px').css('height', '128px'); });
In fact, this callback function parameter is also applicable to basic animations.
With animate(), you can achieve various custom animation effects:
animate()
Serial animation
jQuery Animation effects can also be executed serially and paused through the delay() method. In this way, we can achieve more complex animation effects, but the code is quite simple:
var p = $('#test-animates'); // 动画效果:slideDown - 暂停 - 放大 - 暂停 - 缩小 p.slideDown(2000) .delay(1000) .animate({ width: '256px', height: '256px' }, 2000) .delay(1000) .animate({ width: '128px', height: '128px' }, 2000); }
Because the animation needs to be executed for a period of time, so jQuery must continuously return new Promise objects to perform subsequent operations. Simply encapsulating animations in functions is not enough.
Why some animations have no effect
You may encounter that some animations such as slideUp() have no effect at all. This is because the principle of jQuery animation is to gradually change the value of CSS, such as height gradually changing from 100px to 0. But for many DOM elements that are not block, setting height on them has no effect at all, so the animation will have no effect.
In addition, jQuery does not implement the animation effect of background-color, and setting background-color with animate() has no effect. In this case, you can use CSS3 transition to achieve animation effects.
Related recommendations:
Use canvas to draw a curve animation example
JS and CSS to achieve animation effect sharing during web page loading
Sharing two methods of using jQuery to realize the animation effect of picture sliding doors
The above is the detailed content of Several animation styles built into jQuery. For more information, please follow other related articles on the PHP Chinese website!

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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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.

Dreamweaver CS6
Visual web development tools