欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于vue實(shí)現(xiàn)頁(yè)面滾動(dòng)加載的示例詳解

 更新時(shí)間:2024年01月12日 11:24:17   作者:loyd3  
頁(yè)面內(nèi)容太多會(huì)導(dǎo)致加載速度過(guò)慢,這時(shí)可考慮使用滾動(dòng)加載即還沒(méi)有出現(xiàn)在可視范圍內(nèi)的內(nèi)容塊先不加載,出現(xiàn)后再加載,所以本文給大家介紹了基于vue實(shí)現(xiàn)頁(yè)面滾動(dòng)加載的示例,需要的朋友可以參考下

頁(yè)面內(nèi)容太多會(huì)導(dǎo)致加載速度過(guò)慢,這時(shí)可考慮使用滾動(dòng)加載即還沒(méi)有出現(xiàn)在可視范圍內(nèi)的內(nèi)容塊先不加載,出現(xiàn)后再加載
基本思路就是判斷元素是否出現(xiàn)在瀏覽器的可視區(qū)域,出現(xiàn)了就加載請(qǐng)求接口加載內(nèi)容。但是要求內(nèi)容塊有最小高度。

具體代碼如下

<template>
  <div>
    <div
      v-loading.fullscreen.lock="loading"
      class="page"
      id="tablist"
      @scroll="listScroll"
    >
      <div
        class="item"
        v-for="(item, i) in testData"
        :key="i"
        :class="i % 2 == 0 ? 'gray' : 'white'"
        :id="item.id"
      >
        {{ item.name }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "ScrollLoadingSample",
  props: {},
  data() {
    return {
      flag: true, // 開(kāi)關(guān)
      loading: false, // loading加載
      testData: [], // 列表數(shù)據(jù)
      targetIndex: 0,
      nextId: "",
    };
  },
  mounted() {
    this.initData();
  },
  methods: {
    initData() {
      this.testData = [
        {
          id: "test-1",
          name: "區(qū)塊1",
          // 要加載的方法名
          function: "loadTest1",
          // 方法是否加載了
          isLoaded: false,
        },
        {
          id: "test-2",
          name: "區(qū)塊2",
          function: "loadTest2",
          isLoaded: false,
        },
        {
          id: "test-3",
          name: "區(qū)塊3",
          function: "loadTest3",
          isLoaded: false,
        },
        {
          id: "test-4",
          name: "區(qū)塊4",
          function: "loadTest4",
          isLoaded: false,
        },
        {
          id: "test-5",
          name: "區(qū)塊5",
          function: "loadTest5",
          isLoaded: false,
        },
        {
          id: "test-6",
          name: "區(qū)塊6",
          function: "loadTest6",
          isLoaded: false,
        },
      ];
      this.$nextTick(() => {
        this.loadPage();
      });
    },
    //判斷元素是否在可視區(qū)域
    isElementInViewport(id) {
      let el = document.getElementById(id);
      //獲取元素是否在可視區(qū)域
      let rect = el.getBoundingClientRect();
      return (
        rect.top + rect.height / 2 <=
          (window.innerHeight || document.documentElement.clientHeight) &&
        rect.bottom > 0
      );
    },

    // 初次加載頁(yè)面時(shí)
    loadPage() {
      for (let i = 0; i < this.testData.length; i++) {
        if (this.isElementInViewport(this.testData[i]["id"])) {
          this.loadFunctionByName(this.testData[i]["function"]);
          this.testData[i]["isLoaded"] = true;
        } else {
          this.targetIndex = i;
          // 最開(kāi)始沒(méi)出現(xiàn)在頁(yè)面上的id
          this.nextId = this.testData[this.targetIndex]["id"];
          break;
        } 
      }
    },
    // 頁(yè)面滑動(dòng)
    listScroll() {
      for (let i = this.targetIndex; i < this.testData.length; i++) {
        // 如果出現(xiàn)在頁(yè)面上
        if (this.isElementInViewport(this.testData[i]["id"])) {
          // 如果方法沒(méi)有加載
          if (!this.testData[i]["isLoaded"]) {
            this.loadFunctionByName(this.testData[i]["function"]);
            this.testData[i]["isLoaded"] = true;
          }
        } else {
          // 如果沒(méi)有出現(xiàn)在頁(yè)面上
          this.targetIndex = i;
          this.nextId = this.testData[this.targetIndex]["id"];
          break;
        }
      }
    },
    // 根據(jù)方法名加載
    loadFunctionByName(functionName) {
      switch (functionName) {
        case "loadTest1":
          this.loading = true;
          // 模擬延遲請(qǐng)求
          setTimeout(() => {
            console.log("加載區(qū)塊1");
            this.loading = false;
          }, 1000);
          break;
        case "loadTest2":
          this.loading = true;
          // 模擬延遲請(qǐng)求
          setTimeout(() => {
            console.log("加載區(qū)塊2");
            this.loading = false;
          }, 1000);
          break;
        case "loadTest3":
          this.loading = true;
          // 模擬延遲請(qǐng)求
          setTimeout(() => {
            console.log("加載區(qū)塊3");
            this.loading = false;
          }, 1000);
          break;
        case "loadTest4":
          this.loading = true;
          // 模擬延遲請(qǐng)求
          setTimeout(() => {
            console.log("加載區(qū)塊4");
            this.loading = false;
          }, 1000);
          break;
        case "loadTest5":
          this.loading = true;
          // 模擬延遲請(qǐng)求
          setTimeout(() => {
            console.log("加載區(qū)塊5");
            this.loading = false;
          }, 1000);
          break;
        case "loadTest6":
          this.loading = true;
          // 模擬延遲請(qǐng)求
          setTimeout(() => {
            console.log("加載區(qū)塊6");
            this.loading = false;
          }, 1000);
          break;
      }
    },
  },
};
</script>


<style scoped>
.page {
  width: 100vw;
  height: 100vh;
  overflow-y: auto;
  display: flex;
  flex-wrap: wrap;
  /* justify-content: center;
  align-items: center; */
}
.item {
  width: 100vw;
  height: 25vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
.gray {
  background-color: #d5d1d1;
}
.white {
  background-color: white;
}
.loading {
  width: 80px;
  height: 100px;
  background: rgba(0, 0, 255, 0.664);
  display: inline-block;
}
</style>



需要注意 getBoundingClientRect()這個(gè)方法,正是通過(guò)這個(gè)方法獲取元素位置,從而判斷是否出現(xiàn)在可視區(qū)域

到此這篇關(guān)于基于vue實(shí)現(xiàn)頁(yè)面滾動(dòng)加載的示例詳解的文章就介紹到這了,更多相關(guān)vue頁(yè)面滾動(dòng)加載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論