Animation
Animate elements declaratively with nearly 100 baked‐in presets, or roll your own with custom keyframes. These animations are powered by the Web Animations API.
To animate an element, wrap it in a <pc-animation>
element and set an an animation name. The animation will not start until you add the play
attribute. Refer to the properties table for a list of all animation options.
<div class="animation-overview">
<pc-animation name="bounce" duration="2000" play>
<div class="box"></div>
</pc-animation>
<pc-animation name="jello" duration="2000" play>
<div class="box"></div>
</pc-animation>
<pc-animation name="heartBeat" duration="2000" play>
<div class="box"></div>
</pc-animation>
<pc-animation name="flip" duration="2000" play>
<div class="box"></div>
</pc-animation>
</div>
<style>
.animation-overview .box {
display: inline-block;
width: 100px;
height: 100px;
margin: 1.5rem;
background-color: var(--pc-color-primary-400);
border-radius: var(--pc-border-radius-l);
}
</style>
The animation will only be applied to the first child element found in <pc-animation>
.
Demos
Animations and easings
This demo demonstrates all of the baked‐in animations and easings. Animations are based on those found in the popular Animate.css library.
Intersection Observer
Use an Intersection Observer to control the animation when an element enters or exits the viewport. For example, scroll the box below in and out of your screen. The animation stops when the box exits the viewport and restarts each time it enters the viewport.
Custom keyframes
Supply your own keyframes to build custom animations.
Playing animations programmatically
Animations won’t play until you add the play
attribute. You can omit it initially, then apply it programmatically such as after a user interaction. In this demo, the button will animate once every time the button is clicked.
Properties
Name | Description | Reflects | Default |
---|---|---|---|
name | The name of the built‐in animation to use. For custom animations, use the keyframes prop. string | "none" | |
play | Plays the animation. When omitted, the animation will be paused. This attribute will be automatically removed when the animation finishes or gets cancelled. boolean | false | |
delay | The number of milliseconds to delay the start of the animation. number | 0 | |
direction | Determines the direction of playback as well as the behaviour when reaching the end of an iteration. Learn more about the CSS <code>animation-direction</code> property. PlaybackDirection | "normal" | |
duration | The number of milliseconds each iteration of the animation takes to complete. number | 1000 | |
easing | The easing function to use for the animation. This can be an easing function name like easeInSine or a custom easing function like cubic-bezier(0.12, 0, 0.39, 0) . string | "ease-in-out" | |
endDelay end-delay | The number of milliseconds to delay after the active period of an animation sequence. number | 0 | |
fill | Sets how the animation applies styles to its target before and after its execution. FillMode | "auto" | |
iterations | The number of iterations to run before the animation completes. | Infinity | |
iterationStart iteration-start | The offset at which to start the animation, usually between 0 (start) and 1 (end). number | 0 | |
keyframes | The keyframes to use for the animation. If this is set, the name attribute and property will be ignored. Keyframe[] | undefined | - | |
playbackRate playback-rate | Sets the animation’s playback rate. The default is 1 , which plays the animation at a normal speed. Setting this to 2 , for example, will double the animation’s speed. A negative value can be used to reverse the animation. This value can be changed without causing the animation to restart. number | 1 | |
currentTime | Gets and sets the current animation time. CSSNumberish | - | |
updateComplete | A read‐only promise that resolves when the component has finished updating. | - |
Learn more about attributes and properties.
Slots
Name | Description |
---|---|
(default) | The element to animate. Avoid slotting in more than one element, as subsequent ones will be ignored. To animate multiple elements, either wrap them in a single container or use multiple <pc-animation> elements. |
Learn more about using slots.
Methods
Name | Description | Arguments |
---|---|---|
cancel() | Clears all keyframes caused by this animation and aborts its playback. | - |
finish() | Sets the playback time to the end of the animation corresponding to the current playback direction. | - |
Learn more about methods.
Events
Name | Description | Event detail |
---|---|---|
pc-cancel | Emitted when the animation is cancelled. | - |
pc-finish | Emitted when the animation finishes. | - |
pc‐start | Emitted when the animation starts or restarts. | - |
Learn more about events.
Importing
If you’re using the autoloader or the standard loader, you can ignore this section. If you’re cherry picking, you can use any of the following snippets to import this component.
To import this component from the CDN with a script tag, copy this snippet and paste it in your HTML.
<script type="module" src="https://cdn.jsdelivr.net/npm/placer-toolkit@0.5.1/dist/components/animation/animation.js"></script>
To import this component from the CDN using a JavaScript import, copy this snippet and paste it in your JavaScript:
import "https://cdn.jsdelivr.net/npm/placer-toolkit@0.5.1/dist/components/animation/animation.js";
To import this component with a bundler using a JavaScript import, copy this snippet and paste it in your JavaScript:
import "placer-toolkit/dist/components/animation/animation.js";