105 lines
2.3 KiB
Vue
105 lines
2.3 KiB
Vue
|
||
<template>
|
||
<view class="u-waterfall">
|
||
<view id="u-left-column" class="u-column"><slot name="left" :leftList="leftList"></slot></view>
|
||
<view id="u-right-column" class="u-column"><slot name="right" :rightList="rightList"></slot></view>
|
||
</view>
|
||
</template>
|
||
<script>
|
||
export default {
|
||
props: {
|
||
value: {
|
||
type: Array,
|
||
required: true,
|
||
default: function() {
|
||
return [];
|
||
}
|
||
},
|
||
addTime: {
|
||
type: [Number, String],
|
||
default: 200
|
||
},
|
||
idKey: {
|
||
type: String,
|
||
default: 'id'
|
||
}
|
||
},
|
||
data() {
|
||
return {
|
||
leftList: [],
|
||
rightList: [],
|
||
tempList: [],
|
||
}
|
||
},
|
||
watch: {
|
||
copyFlowList(nVal, oVal) {
|
||
let startIndex = Array.isArray(oVal) && oVal.length > 0 ? oVal.length : 0;
|
||
this.tempList = this.tempList.concat(this.cloneData(nVal.slice(startIndex)));
|
||
this.splitData();
|
||
}
|
||
},
|
||
mounted() {
|
||
this.tempList = this.cloneData(this.copyFlowList);
|
||
this.splitData();
|
||
},
|
||
computed: {
|
||
copyFlowList() {
|
||
return this.cloneData(this.value);
|
||
}
|
||
},
|
||
methods: {
|
||
async splitData() {
|
||
if (!this.tempList.length) return;
|
||
let leftRect = await this.$uGetRect('#u-left-column');
|
||
let rightRect = await this.$uGetRect('#u-right-column');
|
||
// 如果左边小于或等于右边,就添加到左边,否则添加到右边
|
||
let item = this.tempList[0];
|
||
if(!item) return;
|
||
if (leftRect.height < rightRect.height) {
|
||
this.leftList.push(item);
|
||
} else if (leftRect.height > rightRect.height) {
|
||
this.rightList.push(item);
|
||
} else {
|
||
// 这里是为了保证第一和第二张添加时,左右都能有内容
|
||
// 因为添加第一张,实际队列的高度可能还是0,这时需要根据队列元素长度判断下一个该放哪边
|
||
if (this.leftList.length <= this.rightList.length) {
|
||
this.leftList.push(item);
|
||
} else {
|
||
this.rightList.push(item);
|
||
}
|
||
}
|
||
// 移除临时列表的第一项
|
||
this.tempList.splice(0, 1);
|
||
// 如果临时数组还有数据,继续循环
|
||
if (this.tempList.length) {
|
||
setTimeout(() => {
|
||
this.splitData();
|
||
}, this.addTime)
|
||
}
|
||
},
|
||
cloneData(data) {
|
||
return JSON.parse(JSON.stringify(data));
|
||
}
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<style lang="scss" scoped>
|
||
|
||
.u-waterfall {
|
||
display: flex;
|
||
flex-direction: row;
|
||
align-items: flex-start;
|
||
}
|
||
|
||
.u-column {
|
||
display: flex;
|
||
flex: 1;
|
||
flex-direction: column;
|
||
height: auto;
|
||
}
|
||
|
||
.u-image {
|
||
width: 100%;
|
||
}
|
||
</style> |