Several animation styles built into jQuery
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); } </script>
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!

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

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

We often use ppt in our daily work, so are you familiar with every operating function in ppt? For example: How to set animation effects in ppt, how to set switching effects, and what is the effect duration of each animation? Can each slide play automatically, enter and then exit the ppt animation, etc. In this issue, I will first share with you the specific steps of entering and then exiting the ppt animation. It is below. Friends, come and take a look. Look! 1. First, we open ppt on the computer, click outside the text box to select the text box (as shown in the red circle in the figure below). 2. Then, click [Animation] in the menu bar and select the [Erase] effect (as shown in the red circle in the figure). 3. Next, click [

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

Guide to solving misaligned WordPress web pages In WordPress website development, sometimes we encounter web page elements that are misaligned. This may be due to screen sizes on different devices, browser compatibility, or improper CSS style settings. To solve this misalignment, we need to carefully analyze the problem, find possible causes, and debug and repair it step by step. This article will share some common WordPress web page misalignment problems and corresponding solutions, and provide specific code examples to help develop

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

Sometimes we encounter the need to add animation to a ppt. For example, if we want to make a thunderstorm ppt and add some animated thunderstorm effects to it, what should we do? Today, the editor will introduce to you how to make an animated thunderstorm in thunderstorm ppt. It is actually very simple, come and learn it! 1. First we open a PPT page, "Insert" - "Shape" - "Basic Shape" - "Lightning Shape", as shown in the picture. 2. In the "Fill and Line" tab on the right, select "Fill": white; "Select" "Line": black, as shown in the figure. 3. Click "Animation" - "Custom Animation" - "Add Effects" - "Emphasis" - "Subtle" - "Flickering",

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s
