vue动画(transition)的使用及实现动画的方法有哪些

一,实现动画条件:

1, 如果要实现一个元素动画展示,那么当前元素/组件必须是通过v-if v-show 动态组件。

2,动画元素外层必须有内置组件transition包裹起来

3,transition 有个name属性 name属性的值为动画类名

  动态类名分以下几种

  name-enter  name-enter-active  name-enter-to

   name-leave name-leave-active  name-leave-to

二,实现动画方法

      方法1:

<template>
  <div id="app">
    <button @click="show = !show">点击</button>
    <!-- <transition name='fade'>
      <div v-if="show"  class="box"></div>
    </transition> -->
     <!-- <transition name='rotate'>
      <div v-if="show"  class="box"></div>
    </transition> -->
     <!-- <transition name='slider'>
      <div v-if="show"  class="box"></div>
    </transition> -->
      <transition name='move'>
      <div v-if="show"  class="box"></div>
    </transition>
  </div>
</template>

<script>
// import HelloWorld from './components/HelloWorld.vue'
// import OnePage from './components/OnePage.vue'
export default {
  name: 'App',
  components: {
    // HelloWorld,
    // OnePage
  },
  data(){
    return{
      show:true
    }
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
/* 淡出淡入 */
/* .fade-enter-active,.fade-leave-active{
  transition: all 6s ;
}
.fade-enter,.fade-leave-to{
  opacity: 0;
} */


/* 旋转 */
/* .rotate-leave-active{
  transition: all 6s ;
}
.rotate-leave-to{
  transform: rotate(720deg);
} */


/* 滑动 */
/* .slider-enter-active,.slider-leave-active{
  transition: all 3s;
}
.slider-enter,.slider-leave-to{
 height: 0px !important;
} */

/* 移动 */
@keyframes move{
  0% {
    transform: translate(0,0);
  }
  25%{
    transform: translate(200px,0);
  }
  50%{
    transform: translate(200px,200px);
  }
  75%{
    transform: translate(0,200px);
  }
  100%{
    transform: translate(0,0);
  }
}
.move-enter-to,.move-leave-to{
  animation: move 5s 0s linear;
}
.box{
  width: 50px;
  height: 50px;
  background: red;
}
</style>

如果有多个元素动画的时候  我们可以使用transition-group 并加唯一key值

<template>
  <div id="app">
    <button @click="show = !show">点击</button>
    <transition-group name="move">
      <div v-if="show" key="1" class="box"></div>
      <div v-if="show" key="2" class="box"></div>
      <div v-if="show" key="3" class="box"></div>
      <div v-if="show" key="4" class="box"></div>
    </transition-group>
  </div>
</template>

<script>
// import HelloWorld from './components/HelloWorld.vue'
// import OnePage from './components/OnePage.vue'
export default {
  name: "App",
  components: {
    // HelloWorld,
    // OnePage
  },
  data() {
    return {
      show: true,
    };
  },
  methods: {},
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}

/* 移动 */
@keyframes move {
  0% {
    transform: translate(0, 0);
  }
  25% {
    transform: translate(200px, 0);
  }
  50% {
    transform: translate(200px, 200px);
  }
  75% {
    transform: translate(0, 200px);
  }
  100% {
    transform: translate(0, 0);
  }
}
.move-enter-to,
.move-leave-to {
  animation: move 5s 0s linear;
}
.box {
  width: 50px;
  height: 50px;
  background: red;
  margin-bottom: 20px;
}
</style>

 方法2:不用enter和leave实现动画 (可以实现动画不消失要求)

<template>
  <div id="app">
    <button @click="show = !show">点击</button>
    <!-- 不用enter和leave实现动画 -->
    <div :class="show?'box rotate':'box'" ></div>
  </div>
</template>

<script>
// import HelloWorld from './components/HelloWorld.vue'
// import OnePage from './components/OnePage.vue'
export default {
  name: 'App',
  components: {
    // HelloWorld,
    // OnePage
  },
  data(){
    return{
      show:true
    }
  },
  methods:{
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}

.box{
  width: 50px;
  height: 50px;
  background: red;
  transition: all 2s;

}
.rotate{
  transform: rotate(300deg);
}
</style>

 方法3:使用swiper中的animate.css库

1,通过link或import引入该css

 2,代码实现如下

<template>
  <div id="app">
    <button @click="show = !show">点击</button>
   <!-- 使用animate -->
   <div class="box rubberBand animated "></div>
  </div>
</template>

<script>
// import HelloWorld from './components/HelloWorld.vue'
// import OnePage from './components/OnePage.vue'
export default {
  name: 'App',
  components: {
    // HelloWorld,
    // OnePage
  },
  data(){
    return{
      show:true
    }
  },
  methods:{
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}

.box{
  width: 50px;
  height: 50px;
  background: red;
}

</style>

在使用这个animate.css时候,另外一种实现动画方法:提前命名类名

<template>
  <div id="app">
    <button @click="show = !show">点击</button>
  
  <transition
  enter-active-class="animated swing"
  leave-active-class="animated shake"
  >
    <div v-if="show" class="box"></div>
  </transition>
  </div>
</template>

<script>
// import HelloWorld from './components/HelloWorld.vue'
// import OnePage from './components/OnePage.vue'
export default {
  name: 'App',
  components: {
    // HelloWorld,
    // OnePage
  },
  data(){
    return{
      show:true
    }
  },
  methods:{
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}

.box{
  width: 50px;
  height: 50px;
  background: red;
}

</style>

 方法4,其他实现动画的插件:wow.js插件

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
手风琴动画是指在一组元素中,其中一个元素被展开时,其他元素同时被收缩,形成一个类似于手风琴的效果。在 Vue 移动端开发中,可以使用 transition 动画实现手风琴效果。 首先,在 HTML 中定义一组需要展开和收缩的元素,可以使用 v-for 指令来动态渲染元素,并绑定一个 active 变量表示当前展开元素的索引: ``` <div class="accordion"> <div v-for="(item, index) in items" :key="index" :class="{ active: index === active }"> <div class="title" @click="toggle(index)"> {{ item.title }} </div> <div class="content"> {{ item.content }} </div> </div> </div> ``` 其中,toggle 方法用于切换展开和收缩状态: ``` methods: { toggle(index) { this.active = index === this.active ? -1 : index; } } ``` 接下来,在 CSS 中定义手风琴动画的样式,使用 transform 属性实现元素的展开和收缩: ``` .accordion { .title { cursor: pointer; } .content { overflow: hidden; transition: height 0.3s ease-out; &.active { height: auto; transition: height 0.3s ease-in; } } } ``` 其中,active 类用于控制元素的展开和收缩状态,当元素被激活时,添加 active 类,并将高度设置为 auto,实现元素的展开效果;当元素被取消激活时,移除 active 类,并将高度设置为 0,实现元素的收缩效果。 最后,在 Vue使用 transition 组件包裹元素,实现动画效果: ``` <transition name="accordion"> <div v-for="(item, index) in items" :key="index" :class="{ active: index === active }"> <div class="title" @click="toggle(index)"> {{ item.title }} </div> <div class="content"> {{ item.content }} </div> </div> </transition> ``` 其中,name 属性用于指定动画的名称,可以在 CSS 中定义对应的动画样式。 这样就完成了手风琴动画实现

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值