Vue07---vue中的css动画原理及animate.css使用

 

目录

一、transition过渡动画原理

 二、使用keyframes关键帧

 三、在vue中使用animate.css

 同时使用过渡和动画


一、transition过渡动画原理

当元素被transition标签包裹之后,vue会自动分析被包裹的元素的css样式,然后构建一个动画的流程。

在动画开始执行的瞬间会往div标签上增加两个样式:fade-enter、fade-enter-active,当动画第一帧结束开始第二帧的时候,会移除fade-enter样式,同时加上fade-enter-to样式。在动画执行的最后会将fade-enter-active和fade-enter-to都移除。

tip:

transition上的name是起了个名字,不加name属性后续的vue增加的样式就是v-enter和v-enter-active

在动画隐藏的过程中原理如下:

流程和上诉显示过程一样

 

代码例子如下

<template>
  <div>
    <transition name="fade">
      <div v-if="show">hello world</div>
    </transition>
    <button @click="change">切换</button>
  </div>
</template>

<script scoped>
export default {
  name: 'BaseAnimate',
  data () {
    return {
      show: true
    }
  },
  methods: {
    change () {
      this.show = !this.show
    }
  }
}
</script>

<style scoped>
  .fade-enter,.fade-leave-to{
    opacity: 0;
  }
  .fade-enter-active,.fade-leave-active{
    transition: opacity 1s;
  }
</style>

效果:

 二、使用keyframes关键帧

<template>
    <div>
      <transition name="fade">
        <div v-if="show">hello world</div>
      </transition>
      <button @click="change">切换</button>
    </div>
</template>

<script>
export default {
  name: 'keyframes',
  data () {
    return {
      show: true
    }
  },
  methods: {
    change () {
      this.show = !this.show
    }
  }
}
</script>

<style scoped>
  @keyframes bounce-in {
    0% {
      transform: scale(0);
   }
    50% {
      transform: scale(1.5);
   }
    100% {
      transform: scale(1);
   }
  }
//会自动在页面中name为fade的标签上插入这个样式,或者直接在标签上写enter-active-class和leave-active-class,将写好的css样式名字赋上即可
  .fade-enter-active{
    transform-origin: left center;
    animation: bounce-in 1s;
  }
  .fade-leave-active{
    transform-origin: left center;
    animation: bounce-in 1s reverse;
  }
</style>
//也可以使用from to 相当于0%-100%
@keyframes mymove
{
from {top:0px;}
to {top:200px;}
}

效果

 三、在vue中使用animate.css

首先使用npm安装animate.css

npm install animate.css --save

 然后在mian.js里引入

import animate from 'animate.css' 
Vue.use(animate)

代码如下:animated 类似于全局变量,它定义了动画的持续时间,默认1s;swing是动画具体的动画效果的名称,你可以选择任意的效果。具体效果可查看官网文档animate.css动画演示_dowebok

必须使用enter-active-class和leave-active-class

<transition name="fade"
                    enter-active-class="animated swing "
                    leave-active-class="animated shake "
        >
            <div v-if="show">hello world</div>
</transition>

效果:

 同时使用过渡和动画

在enter-active-class后加入自定义的过渡动画

<transition name="fade"
                    type="transition"
                    :duration="{enter:5000,leave:10000}"
//enter和leave分别为入场动画和出场动画的时间
                    appear
                    enter-active-class="animated swing fade-enter-active"
                    leave-active-class="animated shake fade-leave-active"
                    appear-active-class="animated swing"
        >
<!--          type="transition" 设置动画是以transition时间为标准结束还是animate时间为标准结束,animate默认时长是1s-->
<!--          appear-active-class 在页面刷新的时候展示动画效果与appear同时使用-->
<!--         :duration="5000" 自定义动画时长 在此结束后才移除class-->
            <div v-if="show">hello world</div>
</transition>
<style scoped>
 .fade-enter,.fade-leave-to {
        opacity: 0;
    }
    .fade-enter-active {
        transform-origin: left center;
        transition: opacity 1s;
    }
    .fade-leave-active {
        transform-origin: left center;
        transition: opacity 1s;
    }
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Vue Animate.css 是一个 Vue.js 的插件,它可以让你轻松地在 Vue.js 应用程序使用 Animate.css 动画库。使用 Vue Animate.css,你可以通过简单的指令来添加动画效果,而不需要编写任何 JavaScript 代码。下面是使用 Vue Animate.css 的步骤: 1. 安装 Vue Animate.css 插件: ``` npm install vue-animate-css ``` 2. 在 Vue.js 应用程序引入 Vue Animate.css 插件: ``` import Vue from 'vue' import VueAnimateCss from 'vue-animate-css' Vue.use(VueAnimateCss) ``` 3. 在 Vue.js 组件使用指令来添加动画效果: ``` <template> <div v-animate="'bounce'">Hello, world!</div> </template> ``` 在上面的例子,我们使用 `v-animate` 指令来添加 `bounce` 动画效果。你可以在 Vue Animate.css 文档找到更多可用的动画效果。 希望这个回答能够帮助你! ### 回答2: Vue animate.css是一款非常方便实现各种动画的工具。它是基于开源库animate.css开发而来,使得在Vue项目使用动画效果变得更为便捷。 首先,在Vue项目还需要安装vue-animate-css这个插件以便使用这款工具。可以使用npm包管理工具来安装它,输入以下命令然后等待安装完成。 npm install vue-animate-css --save 使用这个工具的方式通常有三种: 1. 以组件的方式使用: 在需要使用动画效果的组件内,引入vue-animate-css库并且设置样式class即可。例如: ``` <template> <div> <h1 :class="animatedClass">例子</h1> </div> </template> <script> import {hover, fadeIn} from 'vue-animate-css'; export default { data: () => ({ animatedClass: hover + ' ' + fadeIn }) } </script> ``` 2. 作为全局 mixin: 使用全局mixin方法后,在整个Vue项目内都可以方便地使用动画效果。比如: ``` import Vue from 'vue'; import {bounce} from 'vue-animate-css'; Vue.mixin({ methods: { bounce: bounce } }); ``` 在使用时只需要在需要使用效果的元素上添加样式类名即可。 3. 通过指令使用: 通过指令使用在有些情况下更为直观。在使用前需要先在Vue项目引入animate.css库。比如: ``` import 'animate.css/animate.min.css'; import Vue from 'vue'; Vue.use({ install(Vue) { Vue.directive('animate', { inserted: (el, binding) => { el.addEventListener('animationend', () => { el.classList.remove(binding.value, 'animated'); }); el.classList.add('animated', binding.value); } }); } }); ``` 在使用指令的组件,可以在需要使用动画效果的元素上添加v-animate指令,比如: ``` <template> <div> <h1 v-animate="'bounce'">例子</h1> </div> </template> ``` 这样就可以实现元素在页面加载时弹跳一次的效果。 总的来说,vue animate.css是一个非常实用的库,可以便捷地实现各种动画效果。具体使用方法可以根据自己的项目需求来灵活运用。 ### 回答3: Vue animate.css是一款能够为Vue应用程序添加动画效果的插件。它通过使用animate.css框架动画类名称,实现了Vue动态展示元素的效果。使用Vue animate.css可以省去手写CSS或Javascript动画的烦琐操作,同时可以方便地实现页面元素的动态效果,提升用户体验。 Vue animate.css使用方法如下: 1. 安装Vue animate.css插件,在命令行运行以下命令: ``` npm install vue-animate-css --save ``` 2. 在Vue组件引入vue-animate-css的插件: ``` import VueAnimateCss from 'vue-animate-css'; Vue.use(VueAnimateCss); ``` 3. 在组件的模板添加需要动画效果的元素,并绑定动画类名称: ``` <template> <div> <h1 v-animate-css="'bounceIn'" class="animated">{{ message }}</h1> </div> </template> ``` 4. 在组件的样式表引入animate.css框架,并为绑定动画类名称的元素添加相应的样式: ``` <style> @import "animate.css"; .animated { animation-duration: 1s; } </style> ``` 5. 在运行应用程序时,就可以看到具有动态效果的元素了。 在以上的使用方法,v-animate-css即为绑定动画类名称的指令,'bounceIn'为animate.css框架的一个动画类名称,可以根据需要自由更换。同时,样式表的animation-duration属性可控制动画持续的时间。 总之,使用Vue animate.css可以在Vue应用程序方便地实现动态效果,增强用户体验,并且简化了开发人员的操作。但需要注意的是,为了保证页面的性能,应适度使用动画效果,以避免过度设计的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值