🚨 $100K in Sight: Follow Bitcoin’s Final Push Live! TRACK NOW

Code has been added to clipboard!

Learn to Run Custom jQuery Animations in Your Web Page

Reading time 3 min
Published Jul 3, 2019
Updated Jan 21, 2020

jQuery animation: Main Tips

  • The jQuery .animate() method runs a custom animation by modifying a set of CSS properties on the selected element.
  • There are two types of syntax you may use.
  • Not all CSS properties can be animated using jQuery .animate(). Check the list of animatable CSS properties.

Two Ways to Use .animate()

The jQuery .animate() runs a custom animation by changing CSS properties on an HTML element. These are altered by gradually increasing or decreasing their value throughout a period of time, which can also be modified.

There are two ways of using this method. You can either define four arguments, or use options argument for the properties of jQuery animations.

DataCamp
Pros
  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid Certificates of completion
edX
Pros
  • A wide range of learning programs
  • University-level courses
  • Easy to navigate
  • Verified certificates
  • Free learning track available
Main Features
  • University-level courses
  • Suitable for enterprises
  • Verified certificates of completion

Specifying Values

The first way to run custom jQuery animations uses syntax like this:

$("selector").animate(styles, speed, easing, function);

The example below shows that only the styles are required to define:

The function takes four arguments:

  • styles is entered in property-value pairs wrapped in curly brackets. The values must also be wrapped in quotes. Property names must be written in camel case.
  • speed - a numeric value, specifying the number of milliseconds that the animation lasts before taking full effect. It can also be a keyword such as slow or fast. If omitted, the default value is 400.
  • easing - a keyword specifying whether the speed of the animation should consistent (linear value) or begin slow and speed up (swing value). If omitted, the default value is swing.
  • function - a callback function to execute once the animation is completed.

Using Options

The second type of .animate() jQuery syntax you can use to run animations looks like this:

$("selector").animate(styles,options);

Two types of arguments are accepted. You enter styles, and then list the options. This argument comes in similar kind of pairs as styles, except that they are properties of the animation:

Options to Include

The syntax for the options argument is a lot like defining a JavaScript object property. Before each function, you need to write the name of the argument value and separate them with commas:

$("selector").animate({styles},{optionName1: value, optionName2: function(){/* code of the function */)});

There are multiple types of options you may use with .animate jQuery method. See them listed and explained here:

Option Description
duration Animation duration in milliseconds, or a keyword value such as fast or slow.
easing A keyword specifying whether the speed of the animation should be consistent (linear) or begin slow and speed up (swing).
queue A boolean value placing the jQuery animation in the queue along with other effects if true and starting it immediately if false.
Since jQuery 1.7, this option can also be defined in a string that represents the name of the specific queue to place the jQuery animation in.
specialEasing An object containing values specified by the styles argument and their corresponding functions for easing.
step A function to be called for every jQuery animation of every element animated using this method.
progress A function to be called for every step of the animation.
complete A function to be called once the animation finishes.
start A function to be called once the animation starts.
done A function to be called once the animation finishes (Promise object is completed).
fail A function to be called in case the animation fails to finish.
always A function to be called no matter whether the animation finishes or not.