files/en-us/web/api/keyframeeffect/index.md
{{ APIRef("Web Animations") }}
The KeyframeEffect interface of the Web Animations API lets us create sets of animatable properties and values, called keyframes. These can then be played using the {{domxref("Animation.Animation", "Animation()")}} constructor.
{{InheritanceDiagram}}
KeyframeEffect object instance, and also allows you to clone an existing keyframe effect object instance.null for animations that do not target a specific element or pseudo-element.null for animations that do not target a pseudo-element.This interface inherits some of its methods from its parent, {{domxref("AnimationEffect")}}.
In the following example, the KeyframeEffect constructor is used to create a set of keyframes that dictate how the rofl emoji should roll on the floor:
const emoji = document.querySelector("div"); // element to animate
const rollingKeyframes = new KeyframeEffect(
emoji,
[
{ transform: "translateX(0) rotate(0)" }, // keyframe
{ transform: "translateX(200px) rotate(1.3turn)" }, // keyframe
],
{
// keyframe options
duration: 2000,
direction: "alternate",
easing: "ease-in-out",
iterations: "Infinity",
},
);
const rollingAnimation = new Animation(rollingKeyframes, document.timeline);
// play rofl animation
rollingAnimation.play();
<div>🤣</div>
body {
box-shadow: 0 5px 5px pink;
}
div {
width: fit-content;
margin-left: calc(50% - 132px);
font-size: 64px;
user-select: none;
margin-top: 1rem;
}
{{ EmbedLiveSample("Examples", "100%", "120") }}
{{Specifications}}
{{Compat}}