重组件的优化和页面渲染十万条数据

重组件的优化和页面渲染十万条数据的优化

重组件的优化

以下代码原理是使用requestAnimationFrame(callback) 方法

vue2写法

Test01.vue

javascript 复制代码
<template>
  <div class="container">
    <div v-for="n in 100" :key="n">
      <HeavyCom v-if="defer(n)"></HeavyCom>
    </div>
  </div>
</template>

<script>
import HeavyCom from "@/views/HeavyCom";

export default {
  components: { HeavyCom },
  data() {
    return {
      frameCount: 0,
    };
  },
  created() {
    this.updateFrameCount();
  },
  methods: {
    updateFrameCount() {
      const maxCount = 100;
      this.frameCount++;
      if (this.frameCount < maxCount) {
        requestAnimationFrame(this.updateFrameCount);
      }
    },
    defer(n) {
      return this.frameCount >= n;
    },
  },
};
</script>

<style>
.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-gap: 1em;
}
</style>

子组件HeavyCom.vue

javascript 复制代码
<template>
  <div class="item-container">
    <div class="item" v-for="n in 5000" :key="n"></div>
  </div>
</template>

<script>
export default {
  name: "HeavyCom"
}
</script>

<style lang="scss">
.item-container {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  border: 3px solid #f40;
  width: 600px;
  height: 600px;
  box-sizing: border-box;
  .item {
    width: 4px;
    height: 4px;
    background: #ccc;
    margin: 1px;
  }
}
</style>

vue3写法

Test01.vue

javascript 复制代码
<template>
  <div class="container">
    <div v-for="n in 100" :key="n">
      <HeavyComp v-if="defer(n)"></HeavyComp>
    </div>
  </div>
</template>
<script setup>
import HeavyComp from "@/views/HeavyComp";
import { useDefer } from './useDefer'
const defer = useDefer();
</script>

<style>
.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-gap: 1em;
}
</style>

子组件HeavyComp.vue

javascript 复制代码
<template>
  <div class="item-container">
    <div class="item" v-for="n in 5000" :key="n"></div>
  </div>
</template>

<style>
.item-container {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  border: 3px solid #f40;
}
.item {
  width: 5px;
  height: 3px;
  background: #ccc;
  margin: 0.1em;
}
</style>

useDefer.js

javascript 复制代码
import {ref, onUnmounted} from "vue";
export function useDefer(maxCount = 100) {
    const frameCount = ref(0);
    let rafId;
    function updateFrameCount() {
        rafId = requestAnimationFrame(() => {
            frameCount.value++
            if (frameCount.value >= maxCount) {
                return;
            }
            updateFrameCount();
        });
    }
    updateFrameCount();
    onUnmounted(() => {
        cancelAnimationFrame(rafId);
    });
    return function defer(n) {
        console.log(frameCount.value, n);
        return frameCount.value >= n;
    }
}

结果:这里为了录制gif不卡顿,只渲染50个重组件

优点: 首屏渲染很快,后续的再依次加载

缺点: 快速滚动时,会出现短暂的白屏

页面渲染十万条数据的优化

使用虚拟列表的方式

通过这张图来表示虚拟列表,红框代表你的手机,黑条代表一条条数据

思路:我们只要知道手机屏幕 最多能放下几条数据 ,当下拉 滑动时,通过双指针 的方式截取相应的数据就可以了。

🚩 PS:为了防止滑动过快 导致的白屏 现象,我们可以使用预加载的方式多加载一些数据出来。

在Test02.vue

javascript 复制代码
<template>
  <div ref="list" class="v-scroll" @scroll="scrollEvent($event)">
    <div class="infinite-list" :style="{ height: listHeight + 'px' }"></div>

    <div class="scroll-list" :style="{ transform: getTransform }">
      <div ref="items" class="scroll-item" v-for="item in visibleData" :key="item.id" :style="{ height: itemHeight + 'px',lineHeight: itemHeight + 'px' }">{{ item.msg }}</div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Test02",
  data() {
    return {
      // 数据
      listData: [],
      // 每项的高度
      itemHeight: 60,
      //可视区域高度
      screenHeight: 600,
      //偏移量
      startOffset: 0,
      //起始索引
      start: 0,
      //结束索引
      end: null,
    };
  },
  computed: {
    //列表总高度
    listHeight() {
      return this.listData.length * this.itemHeight;
    },
    //可显示的列表项数
    visibleCount() {
      return Math.ceil(this.screenHeight / this.itemHeight);
    },
    //偏移量对应的style
    getTransform() {
      return `translate(0,${this.startOffset}px)`;
    },
    //获取真实显示列表数据
    visibleData() {
      return this.listData.slice(this.start, Math.min(this.end, this.listData.length));
    }
  },
  methods: {
    scrollEvent() {
      //当前滚动位置
      let scrollTop = this.$refs.list.scrollTop;
      //此时的开始索引
      this.start = Math.floor(scrollTop / this.itemHeight);
      //此时的结束索引
      this.end = this.start + this.visibleCount;
      //此时的偏移量
      this.startOffset = scrollTop - (scrollTop % this.itemHeight);
    }
  },
  mounted() {
    for (let i = 1; i <= 100000; i++) {
      this.listData.push({id: i, msg: i})
    }
    this.start = 0;
    this.end = this.start + this.visibleCount;
  }
}
</script>

<style>
.v-scroll {
  height: 600px;
  width: 400px;
  border: 3px solid #000;
  overflow: auto;
  position: relative;
  -webkit-overflow-scrolling: touch;
}

.infinite-list {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  z-index: -1;
}

.scroll-list {
  left: 0;
  right: 0;
  top: 0;
  position: absolute;
  text-align: center;
}

.scroll-item {
  padding: 10px;
  color: #555;
  box-sizing: border-box;
  border-bottom: 1px solid #999;
}
</style>

结果:这里为了录制gif不卡顿,只渲染一万条

优点:页面上只渲染出可视区域的内容,非常丝滑,无白屏现象

相关推荐
Highcharts.js1 小时前
Highcharts直方图使用指南:如何根据派生数据创建直方图?
javascript·信息可视化·直方图·highcharts·图表开发·创建图表·派生数据
wuyk5551 小时前
Python办公自动化-第04讲-批量新建Excel
开发语言·python·excel
ZhengEnCi2 小时前
Express 企业级项目结构怎么组织:五层骨架、单向依赖和一份能跑的边界检查脚本
javascript·node.js·express
code2cat2 小时前
【随笔】Java客户端选了HTTP/3,怎样知道请求实际走了哪个版本
java·开发语言·http
泯泷2 小时前
Cloudflare 出手抢 Astro:前端框架之战,底层逻辑变了
前端·javascript·前端框架
cc.ChenLy4 小时前
无法修改 el-dialog 样式原因及解决办法
javascript·vue.js·elementui
狮子座的男孩4 小时前
html+css基础:31、响应式布局(媒体类型,媒体特性,运算符,常用阈值)
前端·css·经验分享·html·响应式布局·媒体类型/特性·常用阈值
鲸鹏芯海微电子科技 (上海) 有限公司4 小时前
手机直连卫星,射频前端到底要解决什么?
前端·智能手机·低轨卫星·謓泽·鲸鹏芯海·毫米波射频·相控阵芯片
Flynt4 小时前
npm run dev --host 有风险?我把 Vite 这个 9.1 分漏洞复现了一遍
前端·vite
AI开源软件4 小时前
Vue3+SpringBoot3 业务配置统一管理:接管、保存与回退怎样保留有效值
java·前端·vue.js·spring boot·vue·ruoyi office