vue长列表,虚拟滚动

1.新建子组件,将数据传递过去(几万条数据的数组,一次性展示多少条,每条数据的行高).

复制代码
<template>
    <div class="vitualScroll">
        <sub-scroll :dataList="dataList" :rowCount="20" :rowHeight="20"></sub-scroll>
    </div>
</template>

<script>
import subScroll from "./components/subScroll.vue"
export default{
    name: 'virtualScroll',
    data(){
        return {
            //冻结数组,初步优化
            dataList: Object.freeze(new Array(20000).fill(null).map((item, index) => ({ n: index+1 })))
        }
    },
    components:{
        subScroll
    }
}
</script>
<style scoped lang="less">
.vitualScroll{
    width: 100%;
    height: 100%;
}
</style>

2.子组件subScroll.vue

div.scrollBar用来使父盒子出现滚动条,div.scrollBar的高度就是数据总条数*单条数据的高度。

div.list才是v-for数据的盒子,采用绝对定位,在父元素滑动事件中更新数据,并且将div.list的位置拉回来(div.list采用绝对定位,滑动滚动会往上跑)

复制代码
<template>
    <div class="scrollView" ref="scrollView" :style="{'--rowHeight': $props.rowHeight + 'px'}" @scroll="onScroll()">
        <div class="scrollBar" :style="{'--scrollBarHeight': $props.dataList.length * $props.rowHeight + 'px'}">

        </div>
        <div class="list" ref="list">
            <div class="item" v-for="(item, index) in copyDataList" :key="index">
                {{ item.n }}
            </div>
        </div>
    </div>
</template>

<script>
export default{
    name: 'subScroll',
    data(){
        return {
            start: 0
        }
    },
    computed:{
        copyDataList(){
            return this.$props.dataList.slice(this.start, this.$props.rowCount + this.start)
        }
    },
    props:{
        dataList: {
            type: Array,
            default(){
                return [{}]
            }
        },
        rowCount: {
            type: Number,
            default: 0
        },
        rowHeight: {
            type: Number,
            default: 0
        }
    },
    mounted(){
        
    },
    methods: {
        onScroll(){
            //全局节流函数
            this.$throttle(this.updataData, 50)
        },
        updataData(){
            let offsetTop = this.$refs.scrollView.scrollTop
            let offsetNum = Math.round(offsetTop / this.$props.rowHeight)
            this.start = offsetNum
            this.$refs.list.style.transform = `translateY(${offsetTop}px)`
            console.log('滑动的距离', offsetTop)

        }
    }
}
</script>

<style lang="less" scoped>
.scrollView{
    width: 200px;
    height: 400px;
    overflow: auto;
    position: relative;
    .item{
        height: var(--rowHeight);
    }
    .scrollBar{
        height: var(--scrollBarHeight);
    }
    .list{
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
    }
}
</style>
相关推荐
Gomiko11 分钟前
JavaScript DOM 原生部分(五):事件绑定
开发语言·前端·javascript
出来吧皮卡丘14 分钟前
A2UI:让 AI Agent 自主构建用户界面的新范式
前端·人工智能·aigc
Jeking21714 分钟前
进阶流程图绘制工具 Unione Flow Editor-- 击破样式痛点:全维度自定义解决方案
前端·流程图·workflow·unione flow·flow editor·unione cloud
晴转多云54315 分钟前
关于Vite后台项目的打包优化(首屏加载)
前端
巴拉巴拉~~19 分钟前
Flutter 通用下拉选择组件 CommonDropdown:单选 + 搜索 + 自定义样式
开发语言·javascript·ecmascript
阿苟19 分钟前
nginx部署踩坑
前端·后端
小林攻城狮21 分钟前
pdfmake 生成平铺式水印:核心方法与优化
前端
search724 分钟前
前端设计:CRG 2--CDC检查
前端·芯片设计
松涛和鸣27 分钟前
DAY33 Linux Thread Synchronization and Mutual Exclusion
linux·运维·服务器·前端·数据结构·哈希算法
逛逛GitHub34 分钟前
我把公众号文章导入了腾讯 ima,可以对话找开源项目了。
前端·github