vue多个元素过度效果案例

本文探讨了两个Vue组件Test.vue和Test2.vue的实现,比较了它们如何使用v-show和v-if控制元素显示与隐藏,并通过transition和transition-group展示了不同动画效果。Test组件采用单个h1元素,而Test2组件则展示了多个元素的切换动画。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Test.vue代码

<template>
  <div>
    <button @click="isShow = !isShow">显示/隐藏</button>
    <transition name="hello" appear>
    <h1 v-show="isShow">你好啊</h1>
    </transition>
  </div>
</template>

<script>
export default {
  name: 'Test',
  data() {
    return {
      isShow:true
    }
  },
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
h1{
  background-color: rebeccapurple;
}

.hello-enter-active{
  animation: 0.1s linear;
}
.hello-leave-active{
  animation: atguigu 0.1s reverse;
}

@keyframes atguigu {
  from{
    transform: translateX(-100%);
  }
  to{
    transform: translateX(0px);
  }
}
</style>

Test2.vue代码

<template>
  <div>
    <button @click="isShow = !isShow">显示/隐藏</button>
    <transition-group name="hello" appear>
      <h1 v-show="!isShow" key="1">你好啊</h1>
      <h1 v-show="isShow" key="2">UTM</h1>
    </transition-group>
  </div>
</template>

<script>
export default {
  name: 'Test',
  data() {
    return {
      isShow:true
    }
  },
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
h1{
  background-color: orange;
}
/* 进入的起点 离开的终点*/
.hello-enter,.hello-leave-to{
  transform: translateX(-100%);
}
.hello-enter-active,.hello-leave-active{
  transition: 0.5s linear;
}
/* 进入的终点 离开的起点*/
.hello-enter-to,.hello-leave{
  transform: translateX(0);
}

</style>

App.vue代码

<template>
  <div>
    <Test/>
    <Test2/>
  </div>
</template>

<script>
import Test from './components/Test.vue'
import Test2 from './components/Test2.vue'

export default {
  name: 'App',
  components: {Test,Test2},
}
</script>

main.js代码

//引入Vue
import Vue from 'vue'
//引入App
import App from './App.vue'
//关闭Vue的生产提示
Vue.config.productionTip = false


//创建vm
new Vue({
  el:'#app',
  render: h => h(App),
})

效果;
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值