这里主要要完成的效果是:当前元素左移消失(过程中透明度逐渐为0),新元素紧随其后左移到上一个元素的位置(透明度在移动的过程中从0到1)。这里出现的问题是两个div并未紧贴着完成动画,而是一直保持这距离,使得整个效果过于生硬。
以下是全部源代码:
Vue 测试实例 - 菜鸟教程(runoob.com).d {
position: relative;
border: 1px solid red;
width: 30px;
height: 30px;
}
@keyframes show {
0% {
opacity: 0;
transform: translateX(30px);
}
100% {
opacity: 1;
transform: translateX(0);
}
}
@keyframes hide {
0% {
opacity: 1;
transform: translateX(0);
}
100% {
opacity: 0;
transform: translateX(-30px);
}
}
.show-enter-active {
animation: show 2s;
}
.show-leave-active {
animation: hide 1s;
}
.show-enter, .show-leave-to {
opacity: 0;
}
.wrap {
display: flex;
}
{{ message }}
{{ item.text }}
add
new Vue({
el: '#app',
data () {
return {
message: 'Hello Vue.js!',
count: 0,
list: [
{id: 0, text: 'aaa'},
{id: 1, text: 'bbb'},
{id: 2, text: 'ccc'}
]
}
},
methods: {
add: function () {
if (this.count < this.list.length - 1) {
this.count += 1;
} else {
this.count = 0;
}
}
}
})