← Back to jQuery Course | Chapter 4: Effects & Animations | Lesson 4 of 9

Custom Animations with animate()

animate changes numeric CSS properties over time.
Syntax
javascript
$(selector).animate({ property: value }, duration, easing, callback);

Basic animate()

The animate() method smoothly transitions one or more numeric CSS properties on an element to new target values over a set duration. It only works on properties with numeric values, like width or opacity, not on colors without a plugin.

Example: Basic animate()

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
      <style>
      #box { background: steelblue; color: white; padding: 10px; border-radius: 4px; }
    </style>
  </head>
  <body>
    <div id="box" style="width:100px">Box</div>
    <button id="btn">Animate</button>
    <button id="resetBtn">Reset</button>
    <div id="output"></div>
    <script>
      $("#btn").on("click", function() {
        $("#output").text("Animating...");
        $("#box").animate({width: "300px"}, function() { $("#output").text("Width is now " + $(this).width() + "px"); });
      });
      $("#resetBtn").on("click", function() {
        $("#box").stop(true).css({width: "100px"});
        $("#output").text("");
      });
    </script>
  </body>
</html>

Multiple Properties

Passing an object with several properties, like {width: 200px, opacity: 0.5}, animates all of them together in one call, saving you from chaining multiple separate .animate() calls that would otherwise run out of sync.

Example: Multiple Properties

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
      <style>
      #box { background: steelblue; color: white; padding: 10px; border-radius: 4px; }
    </style>
  </head>
  <body>
    <div id="box" style="width:100px;opacity:1">Box</div>
    <button id="btn">Animate</button>
    <button id="resetBtn">Reset</button>
    <div id="output"></div>
    <script>
      $("#btn").on("click", function() {
        $("#output").text("Animating...");
        $("#box").animate({width: "200px", opacity: 0.5}, function() { $("#output").text("Width and opacity animated together"); });
      });
      $("#resetBtn").on("click", function() {
        $("#box").stop(true).css({width: "100px", opacity: 1});
        $("#output").text("");
      });
    </script>
  </body>
</html>

Duration

The second argument to animate() sets how long the transition takes, either as milliseconds or a keyword like slow or fast, giving you direct control over animation pacing.

Example: Duration

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
      <style>
      #box { background: steelblue; color: white; padding: 10px; border-radius: 4px; }
    </style>
  </head>
  <body>
    <div id="box" style="width:100px">Box</div>
    <button id="btn">Animate</button>
    <button id="resetBtn">Reset</button>
    <div id="output"></div>
    <script>
      $("#btn").on("click", function() {
        $("#output").text("Animating...");
        $("#box").animate({width: "300px"}, 2000, function() { $("#output").text("Finished after 2 seconds"); });
      });
      $("#resetBtn").on("click", function() {
        $("#box").stop(true).css({width: "100px"});
        $("#output").text("");
      });
    </script>
  </body>
</html>

Opacity

Opacity is one of the few non-pixel numeric properties animate() understands out of the box, letting you fade an element in or out as part of a larger multi-property animation.

Example: Opacity

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
      <style>
      #box { background: steelblue; color: white; padding: 10px; border-radius: 4px; }
    </style>
  </head>
  <body>
    <div id="box" style="">Box</div>
    <button id="btn">Animate</button>
    <button id="resetBtn">Reset</button>
    <div id="output"></div>
    <script>
      $("#btn").on("click", function() {
        $("#output").text("Animating...");
        $("#box").animate({opacity: 0}, function() { $("#output").text("Opacity is now 0"); });
      });
      $("#resetBtn").on("click", function() {
        $("#box").stop(true).css({opacity: 1});
        $("#output").text("");
      });
    </script>
  </body>
</html>

Relative Values

Prefixing a target value with += or -=, like {left: '+=50px'}, animates a property relative to its current value instead of to an absolute one, which is useful for nudging an element by a fixed amount regardless of its starting position.

Example: Relative Values

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
      <style>
      #box { background: steelblue; color: white; padding: 10px; border-radius: 4px; }
    </style>
  </head>
  <body>
    <div id="box" style="position:relative;left:0px">Box</div>
    <button id="btn">Move Right</button>
    <button id="resetBtn">Reset</button>
    <div id="output"></div>
    <script>
      $("#btn").on("click", function() {
        $("#output").text("Animating...");
        $("#box").animate({left: "+=50px"}, function() { $("#output").text("Left is now " + $(this).css("left")); });
      });
      $("#resetBtn").on("click", function() {
        $("#box").stop(true).css({left: "0px"});
        $("#output").text("");
      });
    </script>
  </body>
</html>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Trying to animate non-numeric values, such as .animate({color: "red"}), which jQuery core cannot do without a plugin.
  2. Writing CSS names with hyphens like {background-color: "red"}, which is a syntax error; use camelCase backgroundColor in the object.
  3. Animating width on an element with no set size or display:inline, so nothing seems to happen because inline elements ignore width.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.