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>
相关推荐
叁两6 分钟前
用opencode打造全自动公众号写作流水线,AI 代笔太香了!
前端·人工智能·agent
golang学习记11 分钟前
GitLens 十大神技:彻底改变你在 VS Code 中的 Git 工作流
前端·后端·visual studio code
SuperEugene12 分钟前
后台权限与菜单渲染:基于路由和后端返回的几种实现方式
前端·javascript·vue.js
兆子龙14 分钟前
WebSocket 入门:是什么、有什么用、脚本能帮你做什么
前端·架构
csdn飘逸飘逸14 分钟前
Autojs基础-全局函数与变量(globals)
javascript
是一碗螺丝粉19 分钟前
LangChain 链(Chains)完全指南:从线性流程到智能路由
前端·langchain·aigc
KKKK24 分钟前
手写Promise,从测试用例的角度理解
javascript
月弦笙音24 分钟前
【浏览器】这几点必须懂
前端
青青家的小灰灰25 分钟前
迈向全栈新时代:SSR/SSG 原理、Next.js 架构与 React Server Components (RSC) 实战
前端·javascript·react.js
SuperEugene25 分钟前
弹窗与抽屉组件封装:如何做一个全局可控的 Dialog 服务
前端·javascript·vue.js