Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

KeyframeEffect — a keyframe animation effect

KeyframeEffect describes an effect's target, keyframes, and timing parameters. A separate Animation object controls playback.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

new KeyframeEffect(target, keyframes, options)
new KeyframeEffect(source)

KeyframeEffect is a Web Animations interface, not part of ECMAScript. It inherits from AnimationEffect and describes changes to CSS properties. Construction alone does not start playback: assign the effect to an Animation, whose play() method starts playback. The Element.animate() shortcut creates the effect and animation and starts playback.

target is an element or null. keyframes accepts an array of keyframes or an object containing property values. options is an iteration duration in milliseconds or an object containing settings such as duration, delay, iterations, easing, and fill. Explicit offset values must lie in [0, 1] and be in nondecreasing order. new KeyframeEffect(source) copies the effect definition, not the animation's playback state.

getKeyframes() returns keyframes with computed offsets, and setKeyframes() replaces their set. The inherited updateTiming() changes timing parameters, while getComputedTiming() exposes computed progress. In the example, currentTime = 500 selects the midpoint of a single 1000 ms iteration without playback.

This is a Window API; Node 24 in the repository does not establish browser availability. Check availability in the target environment; the example checks constructors but does not establish support for every option.

Examples

(() => {
  if (typeof KeyframeEffect !== "function" || typeof Animation !== "function") {
    throw new Error("Web Animations API required");
  }
  const target = document.createElement("div");
  const effect = new KeyframeEffect(
    target,
    [{ opacity: 0 }, { opacity: 1 }],
    { duration: 1000, easing: "linear" }
  );
  const animation = new Animation(effect, document.timeline);
  animation.currentTime = 500;
  console.log(effect.getKeyframes().length);
  console.log(effect.getComputedTiming().progress);
  animation.cancel();
})();

Result

2
0.5

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources