Vue3 路由页面切换动画 animate.css

animate.css官网

可以通过以下的内容,来获取自己想要的动态切换样式

在这里插入图片描述

1 路由动画transition或者在组件中控制使用animate

  • 安装
npm install animate.css
  • 在main.ts中引入
import 'animate.css/animate.min.css' //引入动画
  • 使用

在需要切换路由的页面中添加如下内容

html

<router-view v-slot="{ Component }">
   <transition name="router_animate">
     	<component :is="Component" />
   </transition>
</router-view>

css

.router_animate-enter-active {
    animation: slideInLeft 0.5s;
}
.router_animate-leave-active {
    animation: slideOutLeft 0.3s;
}

1.2 呈现结果

在这里插入图片描述

2 用于组件

控制类名是否加在元素上,值得注意的是,元素前面要加上animate__animated的前缀,下图:控制是否显示animate__bounce这个动画,flipinIntrue的时候

const flipinIn = ref(false); 
<el-button type="primary" @click=" flipinIn= true">animate.css实现</el-button>
<h1 class="animate__animated" :class="{animate__bounce:flipinIn}">动画文字</h1>

2.1 呈现结果

在这里插入图片描述

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要根据路由的 meta 属性动态切换 animate.css 的效果,可以使用路由守卫结合动态类绑定的方式来实现。下面以 Vue.js 和 Vue Router 为例,演示如何实现: 1. 首先,在路由配置中定义每个路由的 meta 属性,用于指定要应用的 animate.css 的效果类。 ```javascript const routes = [ { path: '/', component: Home, meta: { animation: 'animate__fadeIn' } }, { path: '/about', component: About, meta: { animation: 'animate__slideInRight' } }, // 其他路由配置... ]; ``` 在上面的例子中,我们为根路径 `'/'` 的路由配置了 `'animate__fadeIn'` 的效果类,而对于 `'/about'` 路由则配置了 `'animate__slideInRight'` 的效果类。你可以根据需要自定义不同的效果类。 2. 在 Vue 组件中的导航守卫中根据路由的 meta 属性动态切换 animate.css 的效果类。 ```javascript // 在 Vue 组件中的路由守卫中动态切换动画效果 export default { // ... beforeRouteEnter(to, from, next) { next(vm => { // 通过 vm.$route.meta.animation 获取当前路由animate.css 效果类 const animation = vm.$route.meta.animation; // 获取要切换的元素,并添加动画效果类 const element = document.getElementById('yourElementId'); element.classList.add('animate__animated', animation); }); }, beforeRouteLeave(to, from, next) { // 在切换路由前,移除动画效果类 const element = document.getElementById('yourElementId'); element.classList.remove('animate__animated'); next(); }, // ... } ``` 在上述代码中,我们在 `beforeRouteEnter` 导航守卫中获取当前路由的 meta 属性,并将其应用到要切换的元素上。通过 `vm.$route` 可以访问到当前路由的信息,包括其 meta 属性。然后,我们获取要切换的元素,并使用 `classList.add` 方法添加 `'animate__animated'` 和动画效果类。 在 `beforeRouteLeave` 导航守卫中,我们在切换路由前移除动画效果类,以确保下次切换时能重新应用新的动画效果。 请确保在要切换的元素上设置了唯一的 id,以便通过 `getElementById` 获取到正确的元素。 通过以上步骤,你就可以根据路由的 meta 属性动态切换 animate.css 的效果了。希望对你有所帮助!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值