vue动画
vue动画需要用transition将所需要过渡的元素包裹起来
//引入vue.js
<script src="./vue-2.4.0.js"></script>
//css代码:
#app {
width: 600px;
margin: auto;
}
.v-enter {
transform: translateX(500px);
}
.v-enter-to {
transform: translateX(0);
}
.v-enter-active {
transition: all 3s;
}
.v-leave {
transform: translateX(0);
}
.v-leave-to {
transform: translateX(-500px);
}
.v-leave-active {
transition: all 3s;
}
.box {
width: 100px;
height: 100px;
background-color: cornflowerblue;
}
.tra-enter {
transform: translateX(-500px);
}
.tra-enter-to,
.tra-leave {
transform: translateX(0);
}
.tra-enter-active,
.tra-leave-active {
transition: all 3s;
}
.tra-leave-to {
transform: translateX(500px);
}
//html代码:
<div id='app'>
<button @click="change">change</button>
<button @click="flag=!flag">changeFlag</button>
<!-- vue.动画需要用transition将所需要的过渡元素包裹起来 -->
<transition>
<div v-show="flag" class="box">{{msg}}</div>
</transition>
<div v-show="flag">{{msg}}</div>
<transition name="tra">
<div v-show="flag" class="box">{{msg}}</div>
</transition>
})
第三方动画
第三方动画需要引入第三方组件
//引入vue.js
<script src="./vue-2.4.0.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" />
//css代码:
#app {
width: 500px;
margin: 200px auto;
}
//html代码:
<div id='app'>
<button @click="show = !show"> render</button>
<transition name="custom-classes-transition" enter-active-class="animate__animated animate__bounceOutUp"
leave-active-class="animate__animated animate__bounceOutUp">
<p v-if="show">hello</p>
</transition>
</div>
//vue代码:
const vm = new Vue({
el: '#app',
data: {
show: true
},
methods: {
}
})
动画钩子函数
//引入vue.js
<script src="./vue-2.4.0.js"></script>
//css代码:
#app {
width: 600px;
margin: auto;
}
.show {
transition: all 0.4s ease;
}
//html代码:
<div id='app'>
<input type="button" value="切换动画" @click="isshow = !isshow">
<transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
<div v-if="isshow" class="show">OK</div>
<!-- <div>123</div> -->
</transition>
</div>
//vue代码:
const vm = new Vue({
el: '#app',
data: {
isshow: true
},
methods: {
beforeEnter(el) { // 动画进入之前的回调
el.style.transform = 'translateX(500px)';
},
enter(el, done) { // 动画进入完成时候的回调
console.log(done);
// 必须添加
el.offsetWidth;
el.style.transform = 'translateX(0px)';
done();
},
afterEnter(el) { // 动画进入完成之后的回调
this.isshow = !this.isshow;
}
}
})
transition-group
//引入vue.js
<script src="./vue-2.4.0.js"></script>
//css代码:
#app {
width: 600px;
margin: auto;
}
.v-enter {
transform: translateX(500px);
}
.v-enter-to {
transform: translateX(0);
}
.v-enter-active {
transition: all 3s;
}
.v-leave {
transform: translateX(0);
}
.v-leave-to {
transform: translateX(-500px);
}
.v-leave-active {
transition: all 3s;
}
//html代码:
<div id='app'>
<button @click="flag = !flag">flag</button>
<button @click="add">add</button>
<transition-group>
<div v-for="(item,index) in list" :key="index" v-show="flag">{{item}}</div>
</transition-group>
</div>
//vue代码:
const vm = new Vue({
el: '#app',
data: {
flag: true,
list: [1, 2, 3, 4]
},
methods: {
add() {
this.list.push(this.list[this.list.length - 1] + 1)
}
}
})
transition-group v-move可以改变定位
//引入vue.js
<script src="./vue-2.4.0.js"></script>
//css代码:
#app {
width: 600px;
margin: auto;
}
.v-enter {
transform: translateX(500px);
}
.v-enter-to {
transform: translateX(0);
}
.v-enter-active {
transition: all 3s;
}
.v-leave {
transform: translateX(0);
}
.v-leave-to {
transform: translateX(-500px);
}
.v-leave-active {
transition: all 3s;
}
.v-move {
transition: all 0.8s ease;
}
.v-leave-active {
position: absolute;
}
.v-enter-active {
position: absolute;
}
//html代码:
<div id='app'>
<button @click="flag = !flag">flag</button>
<button @click="add">add</button>
<transition-group>
<div v-for="(item,index) in list" :key="index" v-show="flag">{{item}}</div>
</transition-group>
</div>
//vue代码:
const vm = new Vue({
el: '#app',
data: {
flag: true,
list: [1, 2, 3, 4]
},
methods: {
add() {
this.list.push(this.list[this.list.length - 1] + 1)
}
}
})