TExcelTAD
Home › jQuery › jQuery Animate

jQuery Animate

.animate() is the engine behind every jQuery effect. You give it a set of numeric CSS properties and their target values, and jQuery moves each one there over time.

js
$(selector).animate({ properties }, duration, easing, callback);
Argument Meaning
properties Object of CSS properties → target values, e.g. { left: "200px", opacity: 0.5 }
duration ms or "fast"/"slow" (default 400)
easing "swing" (default) or "linear"
callback Runs once per element when its animation finishes

A first animation

To move an element with left/top, it must be positioned (position: relative, absolute or fixed) — otherwise those properties have no effect.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <style>
    body { font-family: sans-serif; }
    #stage { position: relative; height: 140px; background: #f4f6f8; border-radius: 6px; margin-top: 8px; }
    #ball { position: absolute; left: 10px; top: 40px; width: 50px; height: 50px; border-radius: 50%; background: #0769ad; }
  </style>
</head>
<body>
  <button id="go">Animate</button>
  <button id="back">Reset</button>
  <div id="stage"><div id="ball"></div></div>

  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <script>
    $("#go").on("click", function () {
      $("#ball").animate({
        left: "250px",
        opacity: 0.4,
        width: "80px",
        height: "80px"
      }, 800, function () {
        console.log("arrived at left =", $(this).css("left"));
      });
    });
    $("#back").on("click", function () {
      $("#ball").stop(true).css({ left: 10, opacity: 1, width: 50, height: 50 });
    });
  </script>
</body>
</html>

Property names and values

  • Write property names in camelCase (fontSize, marginLeft, borderWidth) or quote the hyphenated form ("font-size").
  • Only numeric properties animate: width, height, left, top, margin, padding, opacity, font-size, border-width, scrollTop and similar.
  • Colours do not animate in core jQuery (backgroundColor: "red" jumps or does nothing). Use jQuery UI / the jQuery Color plugin, or a CSS transition.
  • Numbers without a unit are treated as pixels for most properties (left: 200 → 200px).

Relative and keyword values

Value Meaning
"+=50" / "-=50" Add / subtract from the current value
"toggle" Animate to 0 if visible, back to the original if hidden
"show" / "hide" Animate in or out (this is how slideDown etc. are built)
html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <style>
    body { font-family: sans-serif; }
    #box { position: relative; left: 0; width: 60px; height: 60px; background: #e67e22; margin-top: 10px;
           border-radius: 6px; color: #fff; text-align: center; line-height: 60px; }
  </style>
</head>
<body>
  <button id="left">◀ 40px</button>
  <button id="right">40px ▶</button>
  <button id="bigger">Bigger</button>
  <button id="tog">Toggle height</button>
  <div id="box">box</div>

  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <script>
    const log = () => console.log("left:", $("#box").css("left"), "width:", $("#box").width());
    $("#left").on("click", () => $("#box").animate({ left: "-=40" }, 200, log));
    $("#right").on("click", () => $("#box").animate({ left: "+=40" }, 200, log));
    $("#bigger").on("click", () => $("#box").animate({ width: "+=20", fontSize: "+=2" }, 200, log));
    $("#tog").on("click", () => $("#box").animate({ height: "toggle" }, 300));
  </script>
</body>
</html>

The animation queue

When you call .animate() several times on the same element, the animations do not run at the same time — they are put in a queue and run one after another. That makes multi-step movements easy:

html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <style>
    body { font-family: sans-serif; }
    #track { position: relative; width: 300px; height: 150px; border: 1px solid #ccc; margin-top: 8px; }
    #sq { position: absolute; left: 0; top: 0; width: 40px; height: 40px; background: #8e44ad; }
  </style>
</head>
<body>
  <button id="run">Run around the square</button>
  <div id="track"><div id="sq"></div></div>

  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <script>
    $("#run").on("click", function () {
      $("#sq")
        .animate({ left: 260 }, 500)
        .animate({ top: 110 }, 400)
        .animate({ left: 0 }, 500)
        .animate({ top: 0 }, 400, function () {
          console.log("lap complete");
        });
      console.log("queued steps:", $("#sq").queue("fx").length);
    });
  </script>
</body>
</html>

The console.log after the chain runs immediately, while the square is still moving — .animate() only queues work and returns. Click the button several times quickly and the laps pile up in the queue.

The options-object form

For more control, pass an options object as the second argument:

js
$("#box").animate({ left: 200, top: 50 }, {
  duration: 600,
  easing: "linear",
  queue: false,                    // run immediately, in parallel with the queue
  step(now, fx) {                  // called for every property on every frame
    if (fx.prop === "left") $("#pos").text(Math.round(now));
  },
  complete() { console.log("done"); }
});
Option Purpose
duration, easing, complete Same as the positional arguments
queue false to skip the queue, or a queue name
step(now, fx) Run code on every frame for each property
progress(anim, p) Called each frame with progress p from 0 to 1
specialEasing Different easing per property: { left: "linear", top: "swing" }

Tip: $.fx.off = true disables all jQuery animations globally (they jump to the end state) — useful for tests or for users who prefer reduced motion.

Test yourself

Why does $("#box").animate({ left: "200px" }) sometimes do nothing visible?

  1. left must be written as Left
  2. The element is not positioned (it has position: static)
  3. animate only works on images
  4. The duration is missing

Which property can core jQuery NOT animate without a plugin?

  1. opacity
  2. marginLeft
  3. backgroundColor
  4. fontSize

Two .animate() calls are chained on the same element. How do they run?

  1. At the same time
  2. One after the other, through the fx queue
  3. Only the last one runs
  4. In random order