animate-util v1.0.22
动画集成说明
引入
# yarn安装
yarn add animate-util
使用scss自定义函数@function
或@mixin
混入,在使用时只需要在对应的类中引入即可。需配置全局变量文件路径 (推荐) 或者引入样式文件:
配置全局scss变量需安装sass-resources-loader: npm install --save-dev sass-resources-loader
在Webpack中配置:
// 在vue.config.js中添加配置:
module.exports = {
...,
// 配置全局scss变量
chainWebpack: (config) => {
const oneOfsMap = config.module.rule('scss').oneOfs.store;
oneOfsMap.forEach((item) => {
item.use('sass-resources-loader')
.loader('sass-resources-loader')
.options({
// 若是多个文件,改为数组方式传入
// resources: ['file1.scss', 'file2.scss'],
resources: './src/styles/commonVariate.scss',
}).end();
});
},
};
// 在单个样式文件中引入:
@import 'commonAnimate';
在Vite中配置:
import { UserConfigExport } from 'vite'
export default (): UserConfigExport => {
return {
...,
css: {
// css预处理器
preprocessorOptions: {
scss: {
// 定义全局的scss变量
// 给导入的路径最后加上;
additionalData: `@import '@/styles/commonVariate.scss';`
}
}
},
}
}
在全局scss中引入:
// commonVariate.scss
@use 'animate-util/styles/common-animate.scss' as *;
或
// commonVariate.scss
@import '~animate-util/styles/common-animate.scss';
使用场景
1、element-ui部分组件动画效果增强
使用场景:同element-ui
触发方式:用户主动触发
el-switch动画效果增强
.el-switch { $color: #fff; $activeColor: get-color(primary); $inactiveColor: get-color(danger); @include switch-animate1($color, $activeColor, $inactiveColor); } .el-switch { @include switch-animate2(); }
参数 说明 是否必传 默认值 $color 文字颜色 否 #fff(白色) $activeColor 激活时的背景色 否 #409EFF(element-ui主题蓝色) $inactiveColor 非激活时的背景色 否 #909399(element-ui默认灰色) el-checkbox动画效果增强
.el-checkbox { @include checkbox-animate(); }
el-link动画效果增强
.el-link { @include link-animate(); }
2、鼠标悬浮/按下
使用场景:可交互的链接、按钮或图标等
触发方式:用户主动触发
.test-btn {
$baseColor: get-color(primary);
@include transition-color(background-color, $baseColor);
}
.test-text {
$font-color: get-color(danger);
@include transition-color(color, $fontColor);
}
参数 | 说明 | 是否必传 | 默认值 |
---|---|---|---|
$colorType | 颜色类型 | 是 | css中可以为颜色值的任意属性名称 |
$baseColor | 基础颜色 | 是 | 无 |
$hoverColor | 鼠标悬浮时的颜色 | 否 | 基于基础颜色7.5%亮度的颜色 |
$activeColor | 鼠标按下时的颜色 | 否 | 基于基础颜色7.5%暗度, 25%饱和度的颜色 |
样例地址:transition-color (codepen.io)
3、图标按钮悬浮文字说明
使用场景:单个图标按钮或多个图标按钮组,鼠标悬浮时需要显示文字说明,可选用此动画效果。
触发方式:用户主动触发
.button {
$text: 'xx事件';
$height: 60px;
$bgColor: '#f00';
$color: '#fff';
@include tip($text, $height, $bgColor, $color);
}
参数 | 说明 | 是否必传 | 默认值 |
---|---|---|---|
$text | 悬浮显示文字 | 是 | 无 |
$height | 悬浮元素高度 | 是 | 无 |
$bgColor | 悬浮时的背景色 | 是 | 无 |
$color | 文字颜色 | 否 | #fff |
批量设置:
.button {
$height: 60px;
$colorArr: '#409EFF', '#67C23A', '#E6A23C', '#909399';
$btnText: '跌倒事件', '井盖移位事件', '积水事件', '消防占道事件';
@include btn-group($height, $colorArr, $btnText);
}
参数 | 说明 | 是否必传 | 默认值 |
---|---|---|---|
$height | 悬浮元素高度 | 是 | 无 |
$colorArr | 悬浮时的背景色 | 是 | 无 |
$btnText | 悬浮显示文字 | 是 | 无 |
4、带步骤条的表单切换
使用场景:包含步骤条和多个表单,需要进行前后表单切换时,可选用此动画效果。
触发方式:用户间接触发
// 在表单元素组的容器上设置
.animate-step {
@include animate-step('form-view');
}
参数 | 说明 | 是否必传 | 默认值 |
---|---|---|---|
$formClass | 要进行动画的表单类名 | 是 | 无 |
$border | 表单外边距 | 否 | 20px |
import { AnimateUtil } from 'animate-util' // 引入动画工具类
...
data() {
return {
active: 0; // 当前步骤
};
},
methods: {
/**
* @description: 下一步
*/
next() {
animateUtil.stepToNext(this.active, 'form-view');
this.active += 1;
},
/**
* @description: 上一步
*/
prev() {
animateUtil.stepToPrev(this.active, 'form-view');
this.active -= 1;
}
}
参数 | 说明 | 是否必传 | 默认值 |
---|---|---|---|
step | 当前步骤 | 是 | 无 |
formClassName | 要进行动画的表单类名 | 是 | 无 |
样例地址:步骤条表单动画 (codepen.io)
5、悬浮按钮动画
使用场景:鼠标悬浮在容器上才出现按钮的场景。
触发方式:用户间接触发
// 在按钮出现的容器上设置
.card {
$colors: get-color(primary), get-color(danger);
$className: 'icon-btn';
@include hover-icon($colors, $className);
}
参数 | 说明 | 是否必传 | 默认值 |
---|---|---|---|
$colors | 按钮组图标/文字颜色 | 是 | 无 |
$className | 按钮类名 | 是 | 无 |
样例地址:悬浮按钮动画 (codepen.io)
6、呼吸灯动画
触发方式:系统触发/用户主动触发
.breathing-light {
@include breathing-light(get-color(success));
}
参数 | 说明 | 是否必传 | 默认值 |
---|---|---|---|
$fromColor | 呼吸灯最亮点颜色 | 是 | 无 |
$toColor | 呼吸灯最暗点颜色 | 否 | 最亮点25%暗度颜色 |
$shadowWidth | 阴影宽度 | 否 | 5px |
样例地址:呼吸灯效果 (codepen.io)
7、文字摇晃动画
触发方式:用户简介触发
<div class="shake-text">测试文字测试文字测试文字测试文字</div>
import { AnimateUtil } from 'animate-util' // 引入动画工具类
...
const text = document.querySelector('.shake-text');
animateUtil.shake(text);
样例地址:文字摇晃动画 (codepen.io)
8、文字切换动画(短文本)
使用场景:用于不换行的较短文本(推荐20长度以内)切换
<!-- 定义好html格式 -->
<div class="main main1">
<div class="text current"></div>
<div class="text next"></div>
</div>
/* 定义参数 */
$options: (
/* 字体大小 */
font-size: 40px,
/* 每个字符切换速度 */
duration: .24s,
/* 文字颜色 */
color: #000,
/* 最大字数 */
max-length: 15,
/* 是否使用单个字符动画 */
single: true,
/* 汉字高度与数字和英文不一样,如果要切换的文字包含汉字,需要开启此选项 */
isZh: false,
);
/* 引入样式 */
.main {
@include text-switch($options) {
/* 可追加自定义样式 */
font-weight: bold;
};
}
参数 | 说明 | 是否必传 | 默认值 |
---|---|---|---|
font-size | 文字大小 | 否 | 40px |
duration | 单个字符动画速度 | 否 | 0.24s |
color | 文字颜色 | 否 | #000 |
max-length | 最大字数(建议不超过20个字符) | 否 | 15 |
single | 是否需要单个字符动画 | 否 | true |
isZh | 汉字高度与数字和英文不一样,如果要切换的文字包含汉字,需要开启此选项 | 否 | false |
import { TextSwitchUtil } from 'animate-util' // 引入动画工具类
// 初始化工具
const textSwitchUtil = new TextSwitchUtil({
mainClassName: 'main1' // 定义容器类名
});
// 自动切换,切换时间间隔为2000ms
const textArr = ['111111111', '222222222', '333333333', '444444444', '555555555'];
textSwitchUtil.textArrSwitch(textArr, 2000);
// 手动切换
const currentText = 'init';
textSwitchUtil.init(currentText);
...
const nextText = 'next';
textSwitchUtil.doNext(nextText);
数字的切换(没有变化的数字将不会有切换动画):
import { TextSwitchUtil } from 'animate-util' // 引入动画工具类
// 初始化工具
const textSwitchUtil = new TextSwitchUtil({
mainClassName: 'main1' // 定义容器类名
isNumber: true, // 数字类型的切换
});
let number = 0;
textSwitchUtil.init(number);
...
number = 100;
textSwitchUtil3.doNext(number);
9、进入/退出窗口动画
使用场景:从窗口外进入/离开
<!-- 定义好html格式 -->
<div class="translate-view">
<div class="close-btn"></div>
<div class="translate-box"></div>
</div>
/* 定义参数 */
$options: (
/* 执行动画的元素类名 */
box-class-name: 'translate-box',
/* 打开按钮类名 */
btn-class-name: 'close-btn',
/* 动画出现方向(left/right) */
origin: left,
/* 动画持续时间 */
duration: 300ms
);
/* 引入样式 */
.translate-view {
@include hidebox($options);
}
参数 | 说明 | 是否必传 | 默认值 |
---|---|---|---|
box-class-name | 执行动画的元素类名 | 是 | |
btn-class-name | 打开按钮类名 | 是 | |
origin | 动画出现方向(left/right) | 是 | |
duration | 动画持续时间 | 否 | 500ms |
import { AnimateUtil } from 'animate-util' // 引入动画工具类
...
// 传入外部容器
const view = document.querySelector('.translate-view');
// 打开
AnimateUtil.openBox(view);
// 关闭
AnimateUtil.closeBox(view);
样例地址:开窗动画 (codepen.io)
在动画过程中切换动画
通常情况下,在动画运行过程中直接使用css切换动画,会导致动画过渡不自然,产生割裂感。某天在外网的一篇文章中找到了一个解决方案。
文章地址:hacking-css-animation-state-and-playback-time(css-tricks)
// 定义一个变量用于记录动画进度
@property --animate-progress {
syntax: "<number>";
initial-value: 0;
inherits: false;
}
// 旋转动画
@keyframes rotate {
0% {
--animate-progress: 0;
transform: rotate(0deg);
}
100% {
--animate-progress: 1;
transform: rotate(360deg);
}
}
// 折返动画
@keyframes rotateBack {
from {
--animate-progress: 0;
transform: rotate(360deg);
}
to {
--animate-progress: 1;
transform: rotate(0deg);
}
}
import { AnimateUtil } from 'animate-util' // 引入动画工具类
let duration = '10s'; // 动画持续时间
window.onload = () => {
const box = view.querySelector('.box');
doEvent(box, '2s');
box.onmouseenter = () => {
duration = '10s';
doEvent(box, duration);
}
box.onmouseleave = () => {
duration = '2s';
doEvent(box, duration);
}
}
/**
* @description: 开始/切换动画
* @param {htmlElement} element 进行动画的元素
* @param {string} duration 动画时长
*/
const doEvent = (element, duration) => {
let animationName = 'rotate';
animateUtil.animateProgressCheck({
animationElement: element,
animationName,
animationDuration: duration,
animationInfinite: true,
});
}
/**
* @description: 折返动画
* @param {htmlElement} element 进行动画的元素
* @param {string} duration 动画时长
* @param {boolean} back 是否折返
*/
const doBackEvent = (element, duration, back = false) => {
let animationName = 'rotate';
if (back) animationName = animationName === 'rotate' ? 'rotateBack' : 'rotate';
animateUtil.animateProgressCheck({
animationElement: element,
animationName,
animationDuration: duration,
animationInfinite: true,
animationType: 'back'
});
}
目前还存在的缺陷:只能实现 animation-timing-function
属性为 linear
(线性动画)时的无缝切换。
文章推荐
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago