Code has been added to clipboard!

How to Create Impressive Effects Using the jQuery Animate Method

Reading time 3 min
Published Jun 28, 2019
Updated Jan 21, 2020

You can create custom animations by using jQuery animate method. In this jQuery animate tutorial, we will explain the standard syntax and the options provided by the different parameters it takes. You will also learn to create animation sequences.

jQuery Animate: Main Tips

  • The jQuery animate method is used to animate the CSS values of an object.
  • Before you use animate in jQuery, you need to make sure particular values are animatable.

The Method

Animate in jQuery is the most versatile method for applying effects to object. It is not difficult to use as long as you are familiar enough with CSS. This method can be used in various ways, accepting unlimited parameters which specify what CSS properties should be affected, as well as the duration and a callback function (which are both optional).

The syntax pattern for jQuery animation effects is as follows:

$(selector).animate({parameters},duration,callback);

Let's review each of the syntax elements used for animate in jQuery:

  • parameters is the most important part of the animate jQuery method. The parameters are the CSS properties to be animated. They can be absolute or relative values, and the properties should be animatable.
  • duration defines a string or number value that specifies how long (in milliseconds) the animation will last.
  • callback specifies the function that will be called once the method's is executed.

Of course, jQuery animation examples help understand the topic much better than theory. Here we have a basic one to demonstrate how the animation can be used:

In this example, the element #animatedItem will stretch to 200px height in 400 milliseconds, which is the default animation length.

Note: color animations are not a part of the jQuery library, but this can be fixed with the color animations plugin you can find at http://plugins.jquery.com/.

It should be noted that animation can apply to any number of properties at once. Commas separate the different properties with their values (which are surrounded with single quotes). Look at this example to see how it could look like in actual code:

Example
$("button").click(() => {
    $("div").animate({
        left: '500px',
        opacity: '0.25',
        height: '250px',
        width: '100px'
    });
});

Animation Sequences

To create more complex animations you can try to apply multiple animations in a sequence. It is not hard to do: first, you have to assign the element you would like to queue animations for to a variable like this:

var identifier = $(selector);

Next, you use this variable to refer to the element you would like to animate in jQuery, and apply the animate method as many times as you like. See the jQuery animation examples below:

Example
$("button").click(() => {
  var div = $("div"); 
  div.animate({left: '100px'}, "slow");
  div.animate({fontSize: '4em'}, "slow");
});

Relative and Predefined Values

It is possible to modify the CSS properties using relative values. To make the value you assign via the animate jQuery method relative, simply add += to add the value you assign to the pre-existing value and -= to subtract. This is how it would look like in actual code:

Example
$("button").click(() => {
    $("div").animate({
        left: '225px',
        height: '+=100px',
        width: '+=170px'
    });
});

Note: this only works on numerical values.

You can even specify a property's animation value as show, hide, or toggle:

Example
$("button").click(() => {
    $("div").animate({
      width: 'toggle'
    });
});

jQuery Animate: Summary

  • You may use a method called animate in jQuery to animate an object's CSS values.
  • The values you choose should be animatable for you to apply the jQuery animation effects.
Learn jQuery
Introduction
Selectors
Events
Event Methods
Download
Callback
Chaining
Get and Set
Add Element
Remove Element
Traverse
Parent
Children
Sibling Traverse
Filtering
jQuery AJAX
Effects
Show and Hide
Fade Effect
Slide Effect
Animate Effect
Manipulate CSS
Width and Height
.addClass()
.after()
.animate()
.append()
.appendTo()
.attr()
.before()
.bind() (deprecated)
.blur()
.change()
.click()
.clone()
.css()
.dblclick()
.delay()
.delegate() and .undelegate() (deprecated)
.detach()
.empty()
.end()
.error() (deprecated)
.fadeIn()
.fadeOut()
.fadeTo()
.fadeToggle()
.finish()
.focus()
.focusIn()
.focusOut()
.hasClass()
.height()
.hide()
.hover()
.html()
.innerHeight()
.innerWidth()
.insertAfter()
.insertBefore()
.keypress()
.keyup()
.live() and .die() (deprecated)
.load() (deprecated)
.mousedown()
.mouseenter()
.mouseleave()
.mousemove()
.mouseout()
.mouseover()
.mouseup()
.off()
.offset()
.offsetParent()
.on()
.one()
.outerHeight()
.outerWidth()
.position()
.prepend()
.prependTo()
.prop()
.proxy()
.queue()
.ready()
.remove()
.removeAttr()
.removeClass()
.removeProp()
.replaceAll()
.replaceWith()
.resize()
.scroll()
.scrollLeft()
.scrollTop()
.show()
.slideDown()
.slideToggle()
.slideUp()
.stop()
.submit()
.text()
.toggle()
.toggleClass()
.trigger()
.triggerHandler()
.unbind() (deprecated)
.unload() (deprecated)
.unwrap()
.val()
.width()
.wrap()
Event Properties
event.currentTarget
event.preventDefault()
event.relatedTarget
event.stopImmediatePropagation()
event.stopPropagation()
event.target
event.timeStamp
event.type
event.which
jQuery .find()
jQuery .keydown()
jQuery.noConflict()
pageY and pageX