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

🚀 作者主页: 有来技术

🔥 开源项目: youlai-mallvue3-element-adminyoulai-bootvue-uniapp-template

🌺 仓库主页: GitCodeGiteeGithub

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

目录

    • 安装依赖
    • [全局引入 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__animatedanimate__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>

通过点击按钮,切换了两种动画效果:fadeInzoomIn

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

结语

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

相关推荐
天宇&嘘月2 小时前
web第三次作业
前端·javascript·css
小王不会写code2 小时前
axios
前端·javascript·axios
luckyext4 小时前
HBuilderX中,VUE生成随机数字,vue调用随机数函数
前端·javascript·vue.js·微信小程序·小程序
小小码农(找工作版)4 小时前
JavaScript 前端面试 4(作用域链、this)
前端·javascript·面试
鱼樱前端5 小时前
深入JavaScript引擎与模块加载机制:从V8原理到模块化实战
前端·javascript
yangjiajia1234566 小时前
vue3 ref和reactive的区别
前端·javascript·vue.js
诚信爱国敬业友善6 小时前
Vue 基础二(进阶使用)
前端·javascript·vue.js
なし.6 小时前
【Web前端开发精品课 HTML CSS JavaScript基础教程】第二十四章课后题答案
前端·javascript·css·html
随风起舞17 小时前
node.js里的bind,apply, call的区别是什么
前端·javascript·node.js