前端必知必会-Vue 动画Animations-CSS 过渡和动画简介


Vue 动画Animations

Vue 中的内置 <Transition> 组件可帮助我们在使用 v-if、v-show 或动态组件添加或删除元素时制作动画。

在其他情况下使用纯 CSS 过渡和动画没有任何问题。

CSS 过渡和动画简介

本教程的这一部分需要了解基本的 CSS 动画和过渡。

但在我们使用 Vue 特定的内置 <Transition> 组件创建动画之前,让我们先看两个如何在 Vue 中使用纯 CSS 动画和过渡的示例。

示例
App.vue:

<template>
<h1>基本 CSS 过渡</h1>
<button @click="this.doesRotate = true">旋转</button>
<div :class="{ rotate: doesRotate }"></div>
</template>

<script>
export default {
      
      
data() {
      
      
return {
      
      
doesRotate: false
}
}
}
</script>

<style scoped>
.rotate {
      
      
rotate: 160deg;
transition: rotate 1s;
}
div {
      
      
border: solid black 2px;
background-color: lightcoral;
width: 60px;
height: 60px;
}
h1, button, div {
      
      
margin: 10px;
}
</style>

在上面的例子中,我们使用 v-bind 为 <div> 标签赋予一个类,以便它旋转。旋转需要 1 秒的原因是它是用 CSS transition 属性定义的。

在下面的例子中,我们看到了如何使用 CSS animation 属性移动对象。

示例
App.vue:

<template>
<h1>基本 CSS 动画</h1>
<button @click="this.doesMove = true">开始</button>
<div :class="{ move: doesMove }"></div>
</template>

<script>
export default {
      
      
data() {
      
      
return {
      
      
doesMove: false
}
}
}
</script>

<style scoped>
  .move {
      
      
    animation: move .5s alternate 4 ease-in-out;
  }
  @keyframes move {
      
      
    from {
      
      
      translate: 0 0;
    }
    to {
      
      
      translate: 70px 0;
    }
  }
  div {
      
      
    border: solid black 2px;
    background-color: lightcoral;
    border-radius: 50%;
    width: 60px;
    height: 60px;
  }
  h1, button, div {
      
      
    margin: 10px;
  }
</style>

总结

本文介绍了的使用,如有问题欢迎私信和评论

猜你喜欢

转载自blog.csdn.net/qq_24018193/article/details/145748937
今日推荐