项目上需要实现一个无限向上滚动,并且不同透明度的动效,研究了一下transition-group,发现用起来确实非常方便.记录一下transition-group的使用方法.及demo源码.
transition-group 会渲染成一个真实标签,如tag="div" 渲染div标签.
给name属性赋值定义类名前缀,拿name="list"举例:
list-enter : 在元素插入的时候存在,再下一帧移除 (元素的初始状态设置)
list-enter-active : 在元素运动过程中始终存在
list-enter-to : 一开始元素插入时不存在,下一帧添加,在元素运动过程中始终存在(设置动画目标状态)
list-leave: 元素移除时立即生效,下一帧删除(元素删除的初始状态设置)
list-leave-active: 元素移除运动过程中始终存在
list-leave-to: 一开始元素移除不存在,下一帧添加(设置动画移除状态)
list-move: 非插入删除的元素都会存在
demo
<template>
<div class="context-wrap">
<div class="left-arrow arrow"></div>
<transition-group v-if="items !== null" class="list" name="list" tag="div">
<div class="list-item" v-for="item in items" :key="item.index">
{{item.name}}
<span style="color:#FFAD4B;">{{item.value}}</span>{{item.surfix}}
</div>
</transition-group>
<div class="right-arrow arrow"></div>
</div>
</template>
<script>
export default {
data () {
return {
sourceList: createList(), // 原始数据
delay: 3000,
items: null,
nextIndex: 1
}
},
mounted () {
this.items = this.copyList()
this.time = setTimeout(this.toggleIndex, 3000);
},
beforeDestroy () {
if (this.time) {
clearTimeout(this.time)
this.time = null
}
},
methods: {
copyList () {
let list = this.sourceList.map(t => {
return {
...t,
index: this.nextIndex++
}
})
return list
},
toggleIndex () {
let item = {
...this.items[0],
index: this.nextIndex++
}
this.items = [...this.items.splice(1, this.items.length), item]
this.time = setTimeout(this.toggleIndex, this.delay);
}
}
}
function createList () {
let list = []
for (let i = 0; i < 5; i++) {
let item = {
name: '国泰君安对客户A35666556Q强平',
surfix: '张',
value: i + 50
}
list.push(item)
}
return list
}
</script>
<style lang="scss" scoped>
.context-wrap {
width: 100%;
height: 90%;
margin-top: 5%;
margin-left: 20px;
display: flex;
overflow: hidden;
align-items: center;
position: relative;
.list {
width: 286px;
height: 280px;
.list-item {
margin-bottom: 15px;
width: 280px;
height: 40px;
border: 1px solid #071532;
font-size: 14px;
color: #000;
display: flex;
justify-content: center;
align-items: center;
transition: all 3s;
&:nth-child(1) {
opacity: 0.2;
}
&:nth-child(2) {
opacity: 0.5;
}
&:nth-child(3) {
opacity: 1;
}
&:nth-child(4) {
opacity: 0.5;
}
&:nth-child(5) {
opacity: 0.2;
}
&:nth-child(6) {
opacity: 0.2;
}
/** 插入过程 **/
&.list-enter-active {
}
/** 移除过程 **/
&.list-leave-active {
position: absolute;
}
/*** 开始插入、移除结束的位置变化 ***/
// 定义移除的元素目标状态
&.list-leave-to {
opacity: 0;
transform: translateY(-55px);
}
// 定义插入的元素初始状态.
&.list-enter {
opacity: 0;
transform: translateY(55px);
}
}
// 覆盖上面列表元素每个位置的透明度
.list-move {
&:nth-child(2) {
opacity: 0.2;
}
&:nth-child(3) {
opacity: 0.5;
}
&:nth-child(4) {
opacity: 1;
}
&:nth-child(5) {
opacity: 0.5;
}
}
/*** 元素定位改变时动画 ***/
// .list-move {
// transition: all 5s;
// }
}
}
</style>
3
66