shoelace/docs/components/animation.md

179 wiersze
5.5 KiB
Markdown
Czysty Zwykły widok Historia

# Animation
2020-08-10 14:04:25 +00:00
[component-header:sl-animation]
2020-08-10 14:04:25 +00:00
2020-12-29 20:21:15 +00:00
Animate elements declaratively with nearly 100 baked-in presets, or roll your own with custom keyframes. Powered by the [Web Animations API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API).
2020-08-11 22:47:02 +00:00
2020-12-29 20:21:15 +00:00
To animate an element, wrap it in `<sl-animation>` and set a `name` and `duration`. Refer to the [properties table](#properties) for additional options.
2020-08-10 14:04:25 +00:00
```html preview
<div class="animation-overview">
2020-08-11 22:47:02 +00:00
<sl-animation name="bounce" duration="2000"><div class="box"></div></sl-animation>
<sl-animation name="jello" duration="2000"><div class="box"></div></sl-animation>
2020-12-29 20:21:15 +00:00
<sl-animation name="heartBeat" duration="2000"><div class="box"></div></sl-animation>
2020-08-11 22:47:02 +00:00
<sl-animation name="flip" duration="2000"><div class="box"></div></sl-animation>
</div>
<style>
.animation-overview .box {
display: inline-block;
width: 100px;
height: 100px;
2020-12-22 14:40:11 +00:00
background-color: var(--sl-color-primary-500);
2020-08-11 22:47:02 +00:00
margin: 1.5rem;
}
</style>
```
2020-09-02 21:45:59 +00:00
?> The animation will be applied only to the first child element found in `<sl-animation>`.
2020-08-11 22:47:02 +00:00
## Examples
2020-08-10 14:04:25 +00:00
2020-08-12 12:53:11 +00:00
### Animations & Easings
2020-08-10 14:04:25 +00:00
2020-12-29 20:21:15 +00:00
This example demonstrates all of the baked-in animations and easings. Animations are based on those found in the popular [Animate.css](https://animate.style/) library.
2020-08-11 22:47:02 +00:00
```html preview
<div class="animation-sandbox">
<sl-animation name="bounce" easing="ease-in-out" duration="2000">
<div class="box"></div>
</sl-animation>
2020-08-11 22:47:02 +00:00
<div class="controls">
<sl-select label="Animation" value="bounce"></sl-select>
<sl-select label="Easing" value="linear"></sl-select>
<sl-range min="0" max="2" step=".5" value="1"></sl-range>
</div>
</div>
2020-08-10 14:04:25 +00:00
<script>
2020-08-11 22:47:02 +00:00
const container = document.querySelector('.animation-sandbox');
const animation = container.querySelector('sl-animation');
const animationName = container.querySelector('.controls sl-select:nth-child(1)');
const easingName = container.querySelector('.controls sl-select:nth-child(2)');
const playbackRate = container.querySelector('sl-range');
animation.getAnimationNames().then(names => {
names.map(name => {
const menuItem = Object.assign(document.createElement('sl-menu-item'), {
textContent: name,
value: name
});
2020-08-11 22:47:02 +00:00
animationName.appendChild(menuItem);
2020-08-10 14:04:25 +00:00
});
});
2020-08-11 22:47:02 +00:00
animation.getEasingNames().then(names => {
names.map(name => {
const menuItem = Object.assign(document.createElement('sl-menu-item'), {
textContent: name,
value: name
});
easingName.appendChild(menuItem);
});
});
2020-10-09 21:45:16 +00:00
animationName.addEventListener('sl-change', () => animation.name = animationName.value);
easingName.addEventListener('sl-change', () => animation.easing = easingName.value);
playbackRate.addEventListener('sl-change', () => animation.playbackRate = playbackRate.value);
2020-08-11 22:47:02 +00:00
playbackRate.tooltipFormatter = val => `Playback Rate = ${val}`;
</script>
2020-08-11 22:47:02 +00:00
<style>
.animation-sandbox .box {
width: 100px;
height: 100px;
2020-12-22 14:40:11 +00:00
background-color: var(--sl-color-primary-500);
2020-08-11 22:47:02 +00:00
}
2020-08-11 22:47:02 +00:00
.animation-sandbox .controls {
max-width: 300px;
margin-top: 2rem;
}
.animation-sandbox .controls sl-select {
margin-bottom: 1rem;
}
</style>
2020-08-10 14:04:25 +00:00
```
2020-08-12 12:53:11 +00:00
### Using Intersection Observer
Use an [Intersection Observer](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) 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.
```html preview
<div class="animation-scroll">
2020-12-29 20:21:15 +00:00
<sl-animation name="jackInTheBox" duration="2000" iterations="1"><div class="box"></div></sl-animation>
2020-08-12 12:53:11 +00:00
</div>
<script>
const container = document.querySelector('.animation-scroll');
const animation = container.querySelector('sl-animation');
const box = animation.querySelector('.box');
// Watch for the box to enter and exit the viewport. Note that we're observing the box, not the animation element!
const observer = new IntersectionObserver(entries => {
if (entries[0].isIntersecting) {
// Start the animation when the box enters the viewport
animation.pause = null;
} else {
// Reset the animation when the box leaves the viewport
animation.pause = true;
animation.setCurrentTime(0);
}
});
observer.observe(box);
</script>
<style>
.animation-scroll .box {
display: inline-block;
width: 100px;
height: 100px;
2020-12-22 14:40:11 +00:00
background-color: var(--sl-color-primary-500);
2020-08-12 12:53:11 +00:00
}
</style>
```
2020-08-11 22:47:02 +00:00
### Custom Keyframe Formats
Supply your own [keyframe formats](https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API/Keyframe_Formats) to build custom animations.
```html preview
<div class="animation-keyframes">
<sl-animation easing="ease-in-out" duration="2000">
<div class="box"></div>
</sl-animation>
</div>
<script>
const animation = document.querySelector('.animation-keyframes sl-animation');
animation.keyframes = [
2020-12-29 20:21:15 +00:00
{
offset: 0,
easing: 'cubic-bezier(0.250, 0.460, 0.450, 0.940)',
fillMode: 'both',
transformOrigin: 'center center',
transform: 'rotate(0)'
},
{
offset: 1,
easing: 'cubic-bezier(0.250, 0.460, 0.450, 0.940)',
fillMode: 'both',
transformOrigin: 'center center',
transform: 'rotate(90deg)'
}
2020-08-11 22:47:02 +00:00
];
</script>
<style>
.animation-keyframes .box {
width: 100px;
height: 100px;
2020-12-22 14:40:11 +00:00
background-color: var(--sl-color-primary-500);
2020-08-11 22:47:02 +00:00
}
</style>
```
2020-08-10 14:04:25 +00:00
[component-metadata:sl-animation]