「vue3-element-admin」Vue3 + TypeScript 项目整合 Animate.css 动画效果实战指南

🚀 作者主页: 有来技术

🔥 开源项目: youlai-mall︱vue3-element-admin︱youlai-boot︱vue-uniapp-template

🌺 仓库主页: GitCode︱ Gitee ︱ Github

💖 欢迎点赞 👍 收藏 ⭐评论 📝 如有错误敬请纠正!

目录

    • 安装依赖
    • [全局引入 Animate.css](#全局引入 Animate.css)
    • [在组件中使用 Animate.css](#在组件中使用 Animate.css)
    • [💥与 Vue 过渡组件结合使用](#💥与 Vue 过渡组件结合使用)
    • 动态绑定动画效果
    • 结语

在前端开发中,动画效果是提升用户体验的有效手段。开源项目 vue3-element-admin 中,原本使用的是 Element-Plus 内置过渡动画,但这种动画效果比较单一。为了增强用户体验,我们决定将 Animate.css 这一强大的 CSS 动画库整合到 Vue3 + TypeScript 项目中。

本文将详细介绍如何将 Animate.css 集成到基于 Vue3 和 TypeScript 的项目中,从依赖安装到如何在组件中灵活应用动画,帮助你快速上手。🚀

以下步骤参考 Animate.css 官方文档

安装依赖

安装 animate.css 库

bash 复制代码
pnpm add animate.css

全局引入 Animate.css

在 Vue3 + TypeScript 项目中,通常在项目的入口文件中进行全局引入。对于基于 Vite 构建的项目,入口文件一般是 main.ts。

在 main.ts 中加入以下代码来引入 Animate.css:

typescript 复制代码
import { createApp } from 'vue'
import App from './App.vue'

// 全局引入 animate.css
import 'animate.css'

const app = createApp(App)
app.mount('#app')

这样,Animate.css 就会在整个项目中生效,接下来就可以在各个组件中使用动画效果了!

在组件中使用 Animate.css

Animate.css 使用非常简单,只需要在需要动画效果的元素上添加相应的类名即可。注意,Animate.css 4.x 版本中的所有动画类都以 animate__ 为前缀,并且必须与 animate__animated 一起使用。

例如,在组件中:

html 复制代码
<template>
  <div class="animate__animated animate__fadeInDown">
    欢迎使用有来开源后端管理前端模板 vue3-element-admin 
  </div>
</template>

在这个示例中,给 <div> 元素添加了 animate__animated 和 animate__fadeInDown 类,页面加载时会自动应用"向下淡入"动画效果。

更多动画效果参考官方:Animate.css

💥与 Vue 过渡组件结合使用

Animate.css 可以与 Vue 内置的 <Transition> 组件配合使用,实现路由切换和页面过渡动画效果。通过这种方式,可以让页面切换更加平滑和生动。比如,项目 vue3-element-admin 就利用 Animate.css 实现了路由切换时的页面过渡动画效果。

具体实现代码如下,参考自 AppMain.vue:

html 复制代码
<template>
  <section class="app-main">
    <router-view>
      <template #default="{ Component, route }">
        <transition enter-active-class="animate__animated animate__fadeIn" mode="out-in">
          <keep-alive :include="cachedViews">
            <component :is="Component" :key="route.path" />
          </keep-alive>
        </transition>
      </template>
    </router-view>
  </section>
</template>

<script setup lang="ts">
import { useSettingsStore, useTagsViewStore } from "@/store";
import variables from "@/styles/variables.module.scss";

// 缓存页面集合
const cachedViews = computed(() => useTagsViewStore().cachedViews);
</script>

<style lang="scss" scoped>
.app-main {
  position: relative;
  overflow-y: auto;
  background-color: var(--el-bg-color-page);
}
</style>
  • <transition> : 用于包裹需要过渡的组件或页面。enter-active-class 设置进入时的动画类,这里使用了 Animate.css 的 fadeIn 动画。
  • mode="out-in": 使当前页面退出后,才加载新的页面,从而实现更加平滑的过渡效果。

在这个示例中,使用 <Transition> 组件来实现进场动画。当组件出现时,应用 fadeIn 动画,使得交互更加流畅。

更多动画效果参考官方:Animate.css

动态绑定动画效果

Vue3 的响应式数据和条件渲染功能,使得动态切换动画变得十分简单。可以根据某个状态切换不同的动画效果:

html 复制代码
<template>
  <button @click="toggleAnimation">切换动画</button>
  <div :class="['animate__animated', currentAnimation]">
    动画效果展示 
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'

const isActive = ref(true)
const animationA = 'animate__fadeIn'
const animationB = 'animate__zoomIn'

const currentAnimation = computed(() => (isActive.value ? animationA : animationB))

function toggleAnimation() {
  isActive.value = !isActive.value
}
</script>

通过点击按钮,切换了两种动画效果:fadeIn 和 zoomIn。

更多动画效果参考官方:Animate.css

结语

通过本文,掌握如何在 Vue3 + TypeScript 项目中整合 Animate.css。无论是简单的动画效果,还是与 Vue 的动态绑定和过渡效果结合,Animate.css 能的项目更加生动和吸引人。

相关推荐
️公子15 分钟前
可插拔 13 款实时棋牌对战平台实践:房间容器、Plugin SDK 与幂等账本
websocket·typescript·游戏开发·monorepo·插件架构·棋牌平台·实时对战
FungLeo39 分钟前
成为全栈·Vue 管理后台篇·Vue3 + Vite + Naive UI:后台工程基座怎么搭
前端·vue.js·ui·typescript·vite·前端工程化·biome
云上工程笔记1 小时前
SRT字幕一键转手绘动画:srt-whiteboard-animation的复刻实录
大数据·开发语言·javascript·人工智能·机器学习
FungLeo1 小时前
成为全栈·Vue管理后台篇·<scriptsetup>:把页面拆成可读、可测的 Vue 组件
前端·javascript·vue.js·typescript·前端组件化·composition api·vue sfc
清晨Coding14 小时前
删了 Cookie 还能自动登录?Google系网站登录态自动恢复的原理分析
javascript
kyriewen14 小时前
我扫了 814 个依赖,97 个还敢要 Node 10
前端·javascript·程序员
程序员大阳16 小时前
HTML+CSS+JS对话式实战(9)-项目总结与问题分析
javascript·css·html·实战·项目总结
Hilaku17 小时前
MCP 协议会成为前端 AI 工程的标准接口吗?
前端·javascript·程序员
RobinDevNotes19 小时前
从 Electron 到 GPUI,9 个跨平台桌面方案该怎么选
前端·javascript·rust·electron·openresty·前端开发工具
程序员大阳20 小时前
HTML+CSS+JS对话式实战(6)-学生端查看活动功能开发
javascript·css·html·模块·查看学生