zoukankan      html  css  js  c++  java
  • 渲染10万条数据的性能问题

    <!doctype html>
    <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>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.11"></script>
    <script>
        new Vue({
            el: '#app',
            data: {
                list: [
                    '测试数据'
                ],
                startIndex: 0,
                endIndex: 60,
                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 + 60
    
                    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(500000).fill(0)
                    arr = arr.map((item, index) => {
                        return index
                    })
                    this.list = [
                        ...this.list,
                        ...arr
                    ]
                }
            }
        })
    </script>
    </body>
    </html>

     原文:https://www.cnblogs.com/songbw/p/11613869.html

  • 相关阅读:
    bzoj1336: [Balkan2002]Alien最小圆覆盖
    bzoj3564: [SHOI2014]信号增幅仪
    [HDU5353]
    [codeforce1072D]
    [dp001]逛公园
    树上问题泛做
    [BZOJ2599]race
    [CEOI2019]MAGIC TREE
    [BZOJ2836]魔法树
    QTREE3
  • 原文地址:https://www.cnblogs.com/szyicol/p/12881353.html
Copyright © 2011-2022 走看看