.展开收缩时使用的CSS动画完成的(此例子使用了第三种判断方法)
一.首先动态设置class:
1.三目运算符判断
<div class="hello" :class="isActive?'anmDiv':'aaa'"></div>
2.对象式判断
<div class="hello" :class="{ 'aaa': isActive, 'ccc': isSort }" />
3.在computed里面判断
<div class="hello" :class="divStyle"></div>
<script>
export default {
computed:{
divStyle(){
if(this.id===1) {
return 'aaa'
}else if(this.id===2){
return 'ccc'
}
}
},
二.在点击事件里设置改变div的class的属性及相应的操作
changeWidth(){
if(this.id===0 || this.id===2){
this.id=1
}else if(this.id===1){
this.id = 2
}
}
三.在css里面设置动画
<style>
.hello{
width: 100px;
height: 200px;
background: #42b983;
}
.ccc{
transition: 3S;
}
.aaa{
width: 100%;
transition: 3S;
}
</style>
或者:
<style>
.hello{
width: 100px;
height: 200px;
background: #42b983;
}
.ccc{
animation:myfirst1 3s;
-moz-animation:myfirst1 3s; /* Firefox */
-webkit-animation:myfirst1 3s; /* Safari and Chrome */
-o-animation:myfirst1 3s; /* Opera */
}
@keyframes myfirst1 {
from {width:100%;}
to {width:100px;}
}
.aaa{
width: 100%;
animation:myfirst 3s;
-moz-animation:myfirst 3s; /* Firefox */
-webkit-animation:myfirst 3s; /* Safari and Chrome */
-o-animation:myfirst 3s; /* Opera */
}
@keyframes myfirst
{
from {width:100px;}
to {width:100%;}
}
</style>
完整代码:
<template>
<div class="hello1">
<div class="hello" :class="isActive?'anmDiv':'aaa'"></div>
<button @click="changeWidth">change</button>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
isActive: true,
}
},
methods:{
changeWidth(){
if (this.isActive) {
this.isActive = false
} else {
this.isActive = true
}
}
}
}
</script>
<style>
.hello{
width: 100px;
height: 200px;
background: #42b983;
}
.anmDiv{
transition: 3S;
}
.aaa{
width: 100%;
transition: 3S;
}
</style>
运行结果: