search
HomeWeb Front-endJS TutorialShare some commonly used jQuery animation events and animation functions_jquery

Some commonly used jQuery animation functions have been sorted out and are quite useful when making interactive pages

.css('a','12px');
.css({
 a:'12px',
 b:'#fff'
});
.show();
.hide();
.toggle();
.fadeIn();
.fadeOut();
.fadeToggle();
.slideDown();
.slideUp();
.slideToggle();
.text('string');
.animate({
 a:'40px',
 b:'ccc'
},200)
.fadeTo(600,0.4);

Then I sorted out some CSS properties that the animate function can operate, which were actually found from other places on the Internet (http://www.jb51.net/article/75510.htm):

backgroundPosition
 borderWidth
 borderBottomWidth
 borderLeftWidth
 borderRightWidth
 borderTopWidth
 borderSpacing
 margin
 marginBottom
 marginLeft
 marginRight
 marginTop
 outlineWidth
 padding
 paddingBottom
 paddingLeft
 paddingRight
 paddingTop
 height
 width
 maxHeight
 maxWidth
 minHeight
 maxWidth
 font

fontSize (specified in the css parameter of the animate function and is different from the standard css property, for example, this css standard is: font-size. In the same way
This is also the case with many faces)

 bottom
 left
 right
 top
 letterSpacing
 wordSpacing
 lineHeight
 textIndent
 opacity

jQuery animation function

jQuery animation functions are divided into three categories:

1. Basic animation function: both transparent gradient and sliding effect, commonly used animation effects.
2. Sliding animation function: only use sliding effect.
3. Fade animation function: only use the fade effect.

1. Basic animation function:

1. show()

Show hidden matching elements. This is the non-animated version of 'show(speed, [callback])'. If the selected element is visible, this method will not change anything. This method will work regardless of whether the element is hidden via the hide() method or display:none; is set in CSS.
For example: display all paragraphs, $("p").show()

2. show(speed,[callback])

Display matching elements with elegant animation, and optionally return a callback function after the display is completed. Each matched element's height, width, and opacity can be dynamically changed based on a specified speed.
For example: use slow animation to display hidden paragraphs, lasting 600 milliseconds, $("p").show(600)

3. hide()

Hide display elements. This is the non-animated version of 'hide( speed, [callback] )'. If the selected element is hidden, this method will not change anything.
For example: hide all paragraphs, $("p").hide()

4. hide(speed,[callback])

Hide all matching elements with elegant animation and optionally trigger a callback function after display is completed. You can dynamically change the height, width, and opacity of each matched element based on a specified speed. In jQuery1.3, padding and margin will also be animated, and the effect will be smoother.
For example: Use 600ms to slowly hide the paragraph, $("p").hide("slow");

5. toggle

Toggle the visible state of an element. If the element is visible, switch it to hidden; if the element is hidden, switch it to visible.
For example: toggle the visible state of all paragraphs, $("p").toggle()

6. toggle(switch)

The visible state of the cut flower element is based on the switch parameter (true is visible, false is hidden). If switch is set to true, the show() method is called to display the matching element. If switch is set to false, hide() is called to hide the element.
For example: switch the visible state of all paragraphs, varflip=0;$("button").click(function(){$("p").toggle(flip %2==0);});

7. toggle(speed,[callback])

Toggle all matching elements with elegant animation and optionally trigger a callback function after display is completed. Dynamically changes the height, width, and opacity of each matching element according to a specified speed. jquery1.3, padding and margin will also have animations, and the effect will be smoother.

For example: Use 200ms to quickly switch the display state of the paragraph, and then pop up a dialog box, $("p").toggle("fast",function(){alert("hello!");});

2. Sliding animation function sliding

1. slideDown(speed,[callback])

Dynamically display all matching elements through height changes (increase downward), and send a callback function at an optional location after the display is completed. This animation effect only adjusts the height of the element, and can cause the matching elements to be displayed in a "sliding" manner. In jQuery 1.3, the upper and lower padding and margin will also be animated, and the effect will be smoother.
For example: Use 600ms to slowly slide the paragraph down, $("p").slideDown("slow");

2. slideUp(speed,[callback])

Dynamicly hide all matching elements by changing the height (decreasing upward), and optionally triggering a callback function after the hiding is completed.
For example: slowly slide the paragraph up in 600ms, $("p").slideUp("slow");

3. slideToggle(speed,[callback])

Toggle the visibility of all matching elements through height changes, and optionally trigger a callback function after the switch is completed.
For example: slowly slide the paragraph up or down in 600ms, $("p").slideToggle("slow");

3. Fading function Fading

1. fadeIn(speed,[callback])

Achieve the fade-in effect of all matching elements through changes in transparency, and optionally call a callback function after the animation is completed. This animation only adjusts the opacity of the element, which means that the height and width of all matching elements will not change.
For example: Use 600ms to slowly fade the paragraph in, $("p").fadeIn("slow");

2. fadeOut(speed,[callback])

Achieve the fade-out effect of all matching elements through changes in opacity, and optionally trigger a callback function after the animation is completed.
For example: Use 600ms to slowly fade out the paragraph, $("p").fadeOut("slow");

3. fadeTo(speed,opacity,[callback])

把所有匹配元素的不透明度以渐进方式调整到指定的不透明度,并在动画完成后可选的出发一个回调函数。
例如:用600ms缓慢将段落的透明度调整到0.66,大约2/3的可见度,$("p").fadeTo("slow",0.66)

四、自定义动画函数Custom

1、animate(params,[duration],[easing],[callback])用于创建自定义动画的函数。这个函数的关键在于制定动画形式及结果样式属性对象。这个对象中每个属性都表示一个可以变化的样式属性(如height、top或opacity)。注意:所有指定的属性必须用骆驼形式,比如用marginLeft代替margin-left。而每个属性的值表示这个样式属性到多少是动画结束。如果是一个数值,样式属性就会从当前的值渐变到指定的值。如果使用的是hide、show、toggle这样的字符串值,则会就该属性调用默认的动画形式。

例如:点击按钮后div元素的几个不同属性一同变化,

$("#go").click(function(){
$("#block").animate({
width:"90%",height:"100%",fontSize:"10em",borderWidth:10
},1000);
});

2、stop([clearQueue],[gotoEnd])

停止所有在指定元素上正在运行的动画。如果队列中有等待执行的动画(并且clearQueue没有设为true),他们将被马上执行clearQueue(Boolean):如果设置成true,则清空队列。可以立即结束动画。gotoEnd(Boolean):让当前正在执行的动画立即完成,并且重设show和hide的原始样式,调用回调函数等。

例如:点击Go之后开始动画,点Stop之后会在当前位置停下来:

// 开始动画
$("#go").click(function(){
$(".block").animate({left: '+200px'}, 5000);
});
// 当点击按钮后停止动画
$("#stop").click(function(){
$(".block").stop();
});
[javascript] view plaincopy
$(document).ready(function(){ 
$(".box h3").toggle(function(){ 
$(this).next(".text").animate({height: 'toggle', opacity: 'toggle'}, "slow"); 
$(this).addClass("arrow"); 
return false; 
},function(){ 
$(this).next(".text").animate({height: 'toggle', opacity: 'toggle'}, "slow"); 
$(this).removeClass("arrow"); 
return false; 
}); 
}); 
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool