前端开发中使用“虚拟列表”来优化长列表的渲染性能,避免页面卡顿

不管现实多么惨不忍睹,都要持之以恒地相信,这只是黎明前短暂的黑暗而已。不要惶恐眼前的难关迈不过去,不要担心此刻的付出没有回报,别再花时间等待天降好运。真诚做人,努力做事!你想要的,岁月都会给你。前端开发中使用“虚拟列表”来优化长列表的渲染性能,避免页面卡顿,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

什么是虚拟列表

虚拟列表是一种优化大量数据展示的技术,它通过只渲染当前可视区域的数据,避免了大量数据的一次性渲染,从而提高了性能和用户体验。虚拟列表的实现方式有多种,包括虚拟滚动、无限滚动等。

在虚拟滚动中,通过创建一个虚拟的DOM节点来代替实际的数据节点,从而实现对大量数据的渲染和管理。在用户滚动页面时,虚拟DOM节点会根据需要动态地更新和渲染数据。

虚拟列表主要是解决当列表数量较多时(比如上十万条数据、百万万条数据),页面内引入大量的 DOM 元素导致页面卡顿的情况,虚拟列表并非一次性渲染全部列表,而在滚动到页面底部的时候,再去加载剩余的数据。

为什么要使用虚拟列表

在前端开发中,有时候会出现一些不能使用分页来加载数据的情况,当我们需要渲染上万条数据的时候,就可能会出现卡顿的情况,这个时候我们就需要使用虚拟列表。

实现思路
  1. 写一个可视区域的div容器,宽300,高600。
  2. 假设列表每行高30。容器内可视区可以显示20条。
  3. 监听容器滚动scroll。计算可视区第一条的下标,及最后一条的下标。
  4. 取起始下标到结束下标之间的数据,渲染到页面。
  5. 增加个按钮,点击时,模拟5w条数据
全部代码
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .container {
            width: 300px;
            height: 600px;
            overflow: auto;
            border: 1px solid;
            margin: 100px auto;
        }
        .item {
            height: 29px;
            line-height: 30px;
            border-bottom: 1px solid #aaa;
            padding-left: 20px;
        }
    </style>
</head>
<body>
    <div id="app">
        <button @click="add">增加</button>
        <div class="container" ref="container">
            <div class="scroll-wrapper" :style="style">
                <div v-for="(item, index) in scrollList" :key="index" class="item">{{item}}</div>
            </div>
        </div>
    </div>
    <!-- 这里自己引入vue文件 -->
    <script src="./vue-2.5.17.js"></script>
    <script>
        new Vue({
            el: '#app',
            data: {
                list: [],//列表数据
                startIndex: 0,//起始下标
                endIndex: 30,//结束下标
                paddingTop: 0,
                paddingBottom: 0,
                allHeight: 0
            },
            computed: {
                // 取起始下标到结束下标之间的数据,渲染到页面。
                scrollList() {
                    return this.list.slice(this.startIndex, this.endIndex)
                },
                // 设置页面上下边距
                style() {
                    return {
                        paddingTop: this.paddingTop + 'px',
                        paddingBottom: this.paddingBottom + 'px'
                    }
                }
            },
            watch: {
                list(val) {
                    const valLen = val.length
                    this.allHeight = valLen * 30
                    this.paddingBottom = this.allHeight - this.scrollList.length * 30
                }
            },
            mounted() {
                // 监听容器滚动
                const container = this.$refs.container
                container.addEventListener('scroll', () => {
                    const top = container.scrollTop
                    this.startIndex = Math.floor(top / 30)
                    this.endIndex = this.startIndex + 21
                    this.paddingTop = top
                    if (this.endIndex >= this.list.length - 1) {
                        this.paddingBottom = 0
                        return
                    }
                    this.paddingBottom = this.allHeight - 600 - top
                })
            },
            methods: {
                // 模拟数据
                add() {
                    let arr = new Array(50000).fill(0)
                    arr = arr.map((item, index) => {
                        return index
                    })
                    this.list = [
                        ...this.list,
                        ...arr
                    ]
                }
            }
        })
    </script>
</body>
</html>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/188415.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!