外观
外观
怡然
899字约3分钟
2024-05-31
相关信息
Animate.css是一个现成的跨浏览器动画库,有很多内置的动画效果,非常适合一些常见的场景使用,在之前的大屏项目中,曾给组件增加动画效果,就使用的该动画库,能满足大多数需求。里面的动画效果就不一一展示了。
npm install animate.css --save
or
yarn add animate.css
import 'animate.css';
or
<head>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"
/>
</head>
<h1 class="animate__animated animate__bounce">An animated element</h1>
<!--必须使用animate__animated animate__+动画名称-->
可以直接使用动画的@keyframes来修改一些样式
.my-element {
display: inline-block;
margin: 0 0.5rem;
animation: bounce; /* referring directly to the animation's @keyframe declaration */
animation-duration: 2s; /* don't forget to set a duration! */
}
Animate.css使用自定义属性(也称为 CSS 变量)来定义动画的持续时间、延迟和迭代。需要更改动画只需在全局或本地设置一个新值即可。
/* This only changes this particular animation duration */
.animate__animated.animate__bounce {
--animate-duration: 2s;
}
/* This changes all the animations globally */
:root {
--animate-duration: 800ms;
--animate-delay: 0.9s;
}
通过自定义属性,还可以轻松动态更改动画的所有受时间限制的属性,可以使用 javascript 进行设置
// All animations will take twice the time to accomplish
document.documentElement.style.setProperty('--animate-duration', '2s');
// All animations will take half the time to accomplish
document.documentElement.style.setProperty('--animate-duration', '.5s');
默认值
类名 | 默认延迟时间 |
---|---|
animate__delay-2s | 2s |
animate__delay-3s | 3s |
animate__delay-4s | 4s |
animate__delay-5s | 5s |
<div class="animate__animated animate__bounce animate__delay-2s">Example</div>
自定义
/* All delay classes will take 2x longer to start */
:root {
--animate-delay: 2s;
}
/* All delay classes will take half the time to start */
:root {
--animate-delay: 0.5s;
}
默认值
类名 | 默认速度时间 |
---|---|
animate__slow | 2s |
animate__slower | 3s |
animate__fast | 800ms |
animate__faster | 500ms |
<div class="animate__animated animate__bounce animate__faster">Example</div>
自定义
/* All animations will take twice as long to finish */
:root {
--animate-duration: 2s;
}
/* Only this element will take half the time to finish */
.my-element {
--animate-duration: 0.5s;
}
可以控制动画的重复次数
类名 | 默认重复次数 |
---|---|
animate__repeat-1 | 1 |
animate__repeat-2 | 2 |
animate__repeat-3 | 3 |
animate__infinite | 无限次 |
/* The element will repeat the animation 2x
It's better to set this property locally and not globally or
you might end up with a messy situation */
.my-element {
--animate-repeat: 2;
}
animation-fill-mode
animation-fill-mode: both
prefers-reduced-motion
display: inline-block
overflow: hidden
const element = document.querySelector('.my-element');
element.classList.add('animate__animated', 'animate__bounceOutLeft');
const element = document.querySelector('.my-element');
element.classList.add('animate__animated', 'animate__bounceOutLeft');
element.addEventListener('animationend', () => {
// do something
});
const element = document.querySelector('.my-element');
element.style.setProperty('--animate-duration', '0.5s');
const animateCSS = (element, animation, prefix = 'animate__') =>{
// We create a Promise and return it
new Promise((resolve, reject) => {
const animationName = `${prefix}${animation}`;
const node = document.querySelector(element);
node.classList.add(`${prefix}animated`, animationName);
// When the animation ends, we clean the classes and resolve the Promise
function handleAnimationEnd(event) {
event.stopPropagation();
node.classList.remove(`${prefix}animated`, animationName);
resolve('Animation ended');
}
node.addEventListener('animationend', handleAnimationEnd, {once: true});
})
}
// 调用
animateCSS('.my-element', 'bounce');
// or
animateCSS('.my-element', 'bounce').then((message) => {
// Do something after the animation
});