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),
})
效果;