• 封装一个 虚拟列表渲染 组件


    组件代码

    <template>
      <div ref="list" class="infinite-list-container" @scroll="scrollEvent($event)">
        <div class="infinite-list-phantom" :style="{ height: listHeight + 'px' }"></div>
        <div class="infinite-list" :style="{ transform: getTransform }">
          <div ref="items"
            class="infinite-list-item" 
            v-for="item in visibleData" 
            :key="item.id"
            :style="{ height: itemSize + 'px',lineHeight: itemSize + 'px' }"
          >{{ item.value }}</div>
        </div>
      </div>
    </template>
    
    <script>
    export default {
      name:'VirtualList',
      props: {
        //所有列表数据
        listData:{
          type:Array,
          default:()=>[]
        },
        //每项高度
        itemSize: {
          type: Number,
          default:200
        }
      },
      computed:{
        //列表总高度
        listHeight(){
          return this.listData.length * this.itemSize;
        },
        //可显示的列表项数
        visibleCount(){
          return Math.ceil(this.screenHeight / this.itemSize)
        },
        //偏移量对应的style
        getTransform(){
          return `translate3d(0,${this.startOffset}px,0)`;
        },
        //获取真实显示列表数据
        visibleData(){
          return this.listData.slice(this.start, Math.min(this.end,this.listData.length));
        }
      },
      mounted() {
        this.screenHeight = this.$el.clientHeight;
        this.start = 0;
        this.end = this.start + this.visibleCount;
      },
      data() {
        return {
          //可视区域高度
          screenHeight:0,
          //偏移量
          startOffset:0,
          //起始索引
          start:0,
          //结束索引
          end:null,
        };
      },
      methods: {
        scrollEvent() {
          //当前滚动位置
          let scrollTop = this.$refs.list.scrollTop;
          //此时的开始索引
          this.start = Math.floor(scrollTop / this.itemSize);
          //此时的结束索引
          this.end = this.start + this.visibleCount;
          //此时的偏移量
          this.startOffset = scrollTop - (scrollTop % this.itemSize);
        }
      }
    };
    </script>
    
    
    <style scoped>
    .infinite-list-container {
      height: 100%;
      overflow: auto;
      position: relative;
      -webkit-overflow-scrolling: touch;
    }
    
    .infinite-list-phantom {
      position: absolute;
      left: 0;
      top: 0;
      right: 0;
      z-index: -1;
    }
    
    .infinite-list {
      left: 0;
      right: 0;
      top: 0;
      position: absolute;
      text-align: center;
    }
    
    .infinite-list-item {
      padding: 10px;
      color: #555;
      box-sizing: border-box;
      border-bottom: 1px solid #999;
    }
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111

    使用:

    <VirtualList :listData="data" :itemSize="100"/>
    
    • 1
  • 相关阅读:
    社区团购平台(java版)
    Day38—— 509. 斐波那契数 70. 爬楼梯 746. 使用最小花费爬楼梯 (动态规划)
    华为数据治理实践
    redis之分片集群
    关于相机的思考
    使用ffmpeg和sdl播放视频实现时钟同步
    手写一个PrattParser基本运算解析器2: PrattParser概述
    Selenium自动化测试 —— 通过cookie绕过验证码的操作!
    【脑机接口】POINT疗法或将为卒中后失语患者带来新希望!
    ChatGPT的回答从哪里来?
  • 原文地址:https://blog.csdn.net/qq_40137978/article/details/134272906