Detailed explanation of jQuery animation and special effects
In
jquery, besides asynchronous, what we need to use most is animation and special effects. If we want to give users a better experience, jquery is the best choice, let us Let’s take a look at the detailed explanation of jquery’s animation and special effects!
1. Show and hide hide() and show()
For animation, showing and hiding are the most basic One of the effects, this section briefly introduces the display and hiding of jQuery.
<script type="text/javascript"> $(function() { $("input:first").click(function() { $("p").hide(); //隐藏 }); $("input:last").click(function() { $("p").show(); //显示 }); }); </script> <input type="button" value="Hide"> <input type="button" value="Show"> <p>点击按钮,看看效果</p> <div><em>本节主要降级和学习jQuery的自动显隐,渐入渐出、飞入飞出。自定义动画等。 1.显示和隐藏hide()和show() 对于动画来说,显示和隐藏是最基本的效果之一,本节简单介绍jQuery的显示和隐藏。</em> </div>
The above is a test of the hide() and show() functions.
2. Use the show(), hide() and toggle() methods
The previous example uses show() and toggle() The hide() method has been briefly introduced. In fact, these two methods can accept parameters to control the display and hide process.
The syntax is as follows
show(duration,[callback]); hide(duration,[callback]);
Among them, duration represents the length of animation execution time, which can be a string representing speed, including slow, normal, fast. It can also be an integer representing time (milliseconds). callback is an optional callback function. Executed after the animation completes.
<script type="text/javascript"> $(function() { $("input:first").click(function() { $("p").hide(300); //隐藏 }); $("input:last").click(function() { $("p").show(500); //显示 }); }); </script>
The example is the same as the first example, except that time parameters are added to hide() and show(). In fact, toogle() can also add event parameters.
3. Use fadeIn() and fadeOut() methods
For animation effects to show and disappear, jQuery also provides fadeIn( ) fadeOut are two practical methods. Their animation effects are similar to fading, and their syntax is exactly the same as slow() and hide().
fadeIn(duration, [callback]); fadeOut(duration, [callback]);
Example
<script type="text/javascript"> $(function() { $("input:eq(0)").click(function() { $("img").fadeOut(3000); //逐渐fadeOut }); $("input:eq(1)").click(function() { $("img").fadeIn(1000); //逐渐fadeIn }); }); </script> <img src="/static/imghwm/default1.png" data-src="/uploads/allimg/150204/225Q14038-0.jpg" class="lazy" alt="Detailed explanation of jQuery animation and special effects" > <input type="button" value="Hide"> <input type="button" value="Show">
Usage of fadeTo() method.
fadeTo() method gradually changes the opacity of the selected elements to the specified value.
Example:
<script type="text/javascript"> $(function() { $("input:eq(0)").click(function() { $("img").fadeOut(1000); }); $("input:eq(1)").click(function() { $("img").fadeIn(1000); }); $("input:eq(2)").click(function() { $("img").fadeTo(1000, 0.5); }); $("input:eq(3)").click(function() { $("img").fadeTo(1000, 0); }); }); </script> <p><img src="/static/imghwm/default1.png" data-src="03.jpg" class="lazy" alt="Detailed explanation of jQuery animation and special effects" ></p> <input type="button" value="FadeOut"> <input type="button" value="FadeIn"> <input type="button" value="FadeTo 0.5"> <input type="button" value="FadeTo 0">
fadeOut related parameters
speed
Optional. Specifies the speed at which an element changes from the current transparency to the specified transparency.
Possible values:
Milliseconds (e.g. 1500)
"slow"
"normal"
"fast"
opacity Required. Specifies the transparency to fade in or out. Must be a number between 0.00 and 1.00.
callback
Optional. The function to be executed after the fadeTo function is executed.
To learn more about callbacks, visit our jQuery Callback chapter.
This parameter cannot be set unless the speed parameter is set.
4. Slide slideUp and slideDown effects
<script type="text/javascript"> $(function() { $("input:eq(0)").click(function() { $("div").add("img").slideUp(1000); }); $("input:eq(1)").click(function() { $("div").add("img").slideDown(1000); }); $("input:eq(2)").click(function() { $("div").add("img").hide(1000); }); $("input:eq(3)").click(function() { $("div").add("img").show(1000); }); }); </script> <input type="button" value="SlideUp"> <input type="button" value="SlideDown"> <input type="button" value="Hide"> <input type="button" value="Show"><br> <div></div><img src="/static/imghwm/default1.png" data-src="04.jpg" class="lazy" alt="Detailed explanation of jQuery animation and special effects" >
Several mentioned above For animation effects, jQuery also provides slideUp() and slideDown() to simulate a slide-like curtain effect in PPT, which is exactly the same as slow() and hide().
The above code defines a div and an img, which are combined together using the add method.
5. Custom animation
Considering the versatility of the framework and the size of the code file, jQuery cannot cover all animation effects, but It provides the animate() method, which enables developers to customize animations. This section mainly introduces the two forms and applications of the animate() method.
The animate() method gives developers a lot of space. It comes in two forms. The first form is more commonly used. The usage is as follows
animate(params,[duration],[easing],[callback])
where params is the list of css properties that you want to change, and the final value you want to change to, and duration is optional , which has exactly the same meaning as the parameters of show()/hide(). Easing is an optional parameter, usually used by animation plug-ins. Used to control the rhythm change process. jQuery only provides two values: linear and swing. callback is an optional callback function. Fires after the animation completes.
requires attention. Variables in params follow the camel naming method. For example, paddingLeft cannot be written as padding-left. In addition, params can only be attributes represented by numerical values in CSS. For example, properties such as width.top.opacity and
like backgroundColor are not supported by animate.
<style> div { background-color: #FFFF00; height: 40px; width: 80px; border: 1px solid #000000; margin-top: 5px; padding: 5px; text-align: center; } </style> <script type="text/javascript"> $(function() { $("button").click(function() { $("#block").animate({ opacity: "0.5", width: "80%", height: "100px", borderWidth: "5px", fontSize: "30px", marginTop: "40px", marginLeft: "20px" }, 2000); }); }); </script> <button id="go">Go>></button> <div id="block">动画!</div>
In params, jQuery can also use "+=" or "-=" to indicate relative changes. For example,
<style> div { background-color: #FFFF00; height: 40px; width: 80px; border: 1px solid #000000; margin-top: 5px; padding: 5px; text-align: center; position: absolute; } </style> <script type="text/javascript"> $(function() { $("button:first").click(function() { $("#block").animate({ left: "-=80px" //相对左移 }, 300); }); $("button:last").click(function() { $("#block").animate({ left: "+=80px" //相对右移 }, 300); }); }); </script> <button >Go>></button> <button >Go>></button> <div id="block">动画!</div>
first perform absolute positioning of the div, and then use -= and += in animate() to achieve relative left movement and relative right movement respectively.
The animate() method has another form, as shown below:
animate(params,options)
Among them, params is exactly the same as the first form, and options is animation. Optional parameter list, mainly including duration, esaing, callback, queue, etc., among which duration.easing.callback is exactly the same as the first form, queue is a Boolean value, indicating that when there are multiple animate() forming jQuery, the current animate( ) Immediately following the next animate(), should it be executed in sequence (true) or false at the same time?
The following example shows the second usage of animate().
<style> div { background-color: #FFFF22; width: 100px; text-align: center; border: 2px solid #000000; margin: 3px; font-size: 13px; font-family: Arial, Helvetica, sans-serif; } input { border: 1px solid #000033; } </style> <script type="text/javascript"> $(function() { $("input:eq(0)").click(function() { //第一个animate与第二个animate同时执行,然后再执行第三个 $("#block1").animate({ width: "90%" }, { queue: false, duration: 1500 }) .animate({ fontSize: "24px" }, 1000) .animate({ borderRightWidth: "20px" }, 1000); }); $("input:eq(1)").click(function() { //依次执行三个animate $("#block2").animate({ width: "90%" }, 1500) .animate({ fontSize: "24px" }, 1000) .animate({ borderRightWidth: "20px" }, 1000); }); $("input:eq(2)").click(function() { $("input:eq(0)").click(); $("input:eq(1)").click(); }); $("input:eq(3)").click(function() { //恢复默认设置 $("div").css({ width: "", fontSize: "", borderWidth: "" }); }); }); </script> <input type="button" id="go1" value="Block1动画"> <input type="button" id="go2" value="Block2动画"> <input type="button" id="go3" value="同时动画"> <input type="button" id="go4" value="重置"> <div id="block1">Block1</div> <div id="block2">Block2</div>
以上两个div块同时运用了三个动画效果,其中第一个div快的第一个动画添加了queue:false参数,使得前两项两个动画同时执行。可以通过重置反复测试,熟悉animate()第二种形式。
以上就是本文的全部内容了,希望大家能够喜欢。
相关推荐:
The above is the detailed content of Detailed explanation of jQuery animation and special effects. For more information, please follow other related articles on the PHP Chinese website!

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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

Dreamweaver Mac version
Visual web development tools

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