Home Web Front-end JS Tutorial A guide to writing animation effects in JavaScript's jQuery library_Basics

A guide to writing animation effects in JavaScript's jQuery library_Basics

May 16, 2016 pm 03:45 PM
jquery animation

The commonly used animation methods in jquery are hide() and show().

$(element).hide() This code can be equivalent to this element.css("display","none")

Fill in the events in hide(time) and show(time), and they can slowly disappear and appear. You can modify multiple styles, height, width, and opacity of elements.

Another set of methods fadeIn() and fadeOut() are different from hide and show in that when hide or show is used, the height of the web page will be changed, while fadeIn and fadeOut will not.


$("#panel h5.head").toggle(function(){                                                                                        $ (this).removeClass("highlight");                                                                                                                                                                                                                                                                                          .


Summary of animation methods

2015813161335011.jpg (631×495)

Animation Queue


(1) Animation effects on a set of elements.


a) When applying multiple properties in one animate() method, animations occur simultaneously.


b) When the animation method is applied in a chained manner, the animation occurs in sequence.


(2) Animation effects on multiple groups of elements


a) By default, animations all occur simultaneously.


b) When the animation method is applied in the form of callbacks, the animation occurs in the order of the callbacks.


In addition, in the animation method, be aware that other non-animation methods will jump in the queue, such as the css() method. To make these non-animation methods also execute in order, these methods need to be written in the callback function of the animation method.


Give an example of animate:

1

2

3

4

5

6

7

$(“#id”).animat({left:”400px”,top:”300px”},3000,function(){

 

 

     $(this).css(“border”,”1px solid blue”);

 

 

});

Copy after login



If you want the animation to stop, you need to insert the stop() method

before the animate() method


For example: $("#id").stop().animate() Pay attention to the two parameters in stop.


Method to determine whether an element is in animated state:

1

$(element).is(“:animated”);

Copy after login


jQuery can easily add some dynamic effects to elements on the page. You can use its built-in effects or you can define your own effects.

Here are some built-in effect methods:

  • $.fn.show Show selected element
  • $.fn.hide Hide the selected element
  • $.fn.fadeIn fade in
  • $.fn.fadeOut fadeout
  • $.fn.slideDown displays elements through vertical sliding effect
  • $.fn.slideUp hides elements through the vertical China effect
  • $.fn.slideToggle Show or hide sliding interaction execution

A simple example:

1

$('h1').show();

Copy after login

Set the duration of the animation effect

For $.fn.show and $.fn.hide, the default duration is 0. The default duration for other effects is generally 400 milliseconds. Of course, you can also set the duration yourself:

1

2

$('h1').fadeIn(300);   // 300 毫秒

$('h1').fadeOut('slow'); // slow 是内建的速度常量

Copy after login

jQuery’s default speed constants are located in the jQuery.fx.speeds object:

1

2

3

4

5

6

speeds: {

  slow: 600,

  fast: 200,

  // Default speed

  _default: 400

}

Copy after login

We can also extend this object and add our own commonly used speed values:

1

2

jQuery.fx.speeds.blazing = 100;

jQuery.fx.speeds.turtle = 2000;

Copy after login

Callback function

If you want to execute some code after the animation effect ends, you can replace these animation methods with a callback function:

1

2

3

$('div.old').fadeOut(300, function() {

 $(this).remove();

});

Copy after login

If no element is matched in the selector, the callback function will not be executed, so it is necessary to make a judgment before executing the callback function:

1

2

3

4

5

6

7

8

9

10

11

var $thing = $('#nonexistent');

 

var cb = function() {

  console.log('done!');

};

 

if ($thing.length) {

  $thing.fadeIn(300, cb);

} else {

  cb();

}

Copy after login

Custom animation method

The $.fn.animate method in jQuery can be used to extend our custom animation. This is mainly achieved by setting the CSS properties of elements through the animate method. When setting the CSS properties of elements, you can use absolute values ​​or relative values. Value:

1

2

3

4

5

6

7

8

$('div.funtimes').animate(

  {

    left : "+=50",

    opacity : 0.25

  },

  300, // 时长

  function() { console.log('done!'); // 回调函数

});

Copy after login

However, when using $.fn.animate to create a custom animation effect, the color of the element cannot be changed. If you want to create color animations, you need to rely on some other color plug-ins.
Animation style

jQuery has two built-in animation styles: swing and linear

1

2

3

4

5

6

7

$('div.funtimes').animate(

  {

    left : [ "+=50", "swing" ],

    opacity : [ 0.25, "linear" ]

  },

  300

);

Copy after login

Control animation

jQuery provides several methods to control the execution of animation:

$.fn.stop stops the currently executing animation

$.fn.delay pauses the animation for a period of time:

$('h1').show(300).delay(1000).hide(300);

jQuery.fx.off: Turn off the transition effect of animation, which is equivalent to setting the duration to 0.


Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to set up ppt animation to enter first and then exit How to set up ppt animation to enter first and then exit Mar 20, 2024 am 09:30 AM

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 [

Detailed explanation of jQuery reference methods: Quick start guide Detailed explanation of jQuery reference methods: Quick start guide Feb 27, 2024 pm 06:45 PM

Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

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

How to remove the height attribute of an element with jQuery? How to remove the height attribute of an element with jQuery? Feb 28, 2024 am 08:39 AM

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

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

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: &lt

How to make animated thunderstorm in thunderstorm ppt How to make animated thunderstorm in thunderstorm ppt Mar 20, 2024 pm 02:41 PM

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",

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

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:

How to tell if a jQuery element has a specific attribute? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

See all articles