VirtualList.vue 2.97 KB
<template>
    <div class="container clearfix" ref="containerRef" style="height: 100%">
        <div class="empty" :style="{ height: emptyHeight }"></div>
        <div :style="{ transform: `translateY(${translateY})` }">
            <el-row v-for="row in showedList">
                <el-col :span="8" v-for="item in row" @click="selectImage(item.id)">
                    <div v-bind:class="{ 'isSelected': item.isSelected}"
                         class="single-image-parent flex-vertical-center"
                         @contextmenu.prevent=""
                         @click.right="previewImage(getImageUrl(item.picId))">

                        <el-image :src="getImageUrl(item.picId)"
                                  :fit="'contain'"
                                  class="single-image">
                        </el-image>
                    </div>
                    <div>
                        {{ item.createTime }} {{ getDirectionTitle(item.direction) }}
                    </div>

                </el-col>
            </el-row>
        </div>

    </div>
</template>

<script>
import {computed, onMounted, ref} from 'vue'
import {getImageUrl, getPagedList, previewImage} from '@/PublicUtil/PublicUtil'

export default {
    props: {
        dataList: {
            default: []
        },
    },
    setup(props, {emit}) {
        const dataList = props.dataList

        const containerRef = ref()
        const itemHeight = 326

        const start = ref(0)
        const translateY = ref(0)

        const showedList = computed(
            () => {
                return dataList.slice(start.value, start.value + 3)
            }
        )

        const emptyHeight = itemHeight * (dataList.length + 1)

        const getDirectionTitle = function(v) {
            switch (v)
            {
                case 1:
                {
                    return '进'
                }
                case -1:
                {
                    return '出'
                }
                default:
                {
                    break
                }
            }
        }
        const selectImage = function(id) {
            emit('imageClick', id)
        }

        onMounted(
            () => {
                containerRef.value.addEventListener(
                    'scroll',
                    (event) => {
                        const {scrollTop} = event.target
                        start.value = Math.floor(scrollTop / itemHeight)
                        translateY.value = scrollTop + 'px'
                    }
                )
            }
        )

        return {
            containerRef,
            translateY,
            itemHeight: itemHeight + 'px',
            emptyHeight: emptyHeight + 'px',
            // sequence
            showedList,
            // function
            selectImage,
            getDirectionTitle,
            getImageUrl,
            previewImage,
        }
    }
}
</script>

<style lang="less" scoped>
@import "./VirtualList";
</style>