最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue虛擬滾動(dòng)/虛擬列表簡(jiǎn)單實(shí)現(xiàn)示例

 更新時(shí)間:2025年01月10日 09:30:48   作者:llh_fzl  
本文主要介紹了vue虛擬滾動(dòng)/虛擬列表簡(jiǎn)單實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

App.vue

<template>
  <div class="container">
    <virtual-list :data="data"/>
  </div>
</template>

<script setup>
import { reactive } from 'vue';
import VirtualList from './components/VirtualList.vue';

const data = reactive((() => {
  const arr = [];
  for (let i = 0; i < 1000000; i++) arr[i] = i;
  return arr;
})());
</script>

<style scoped>
.container {
  width: 400px;
  height: 400px;
}
</style>

virtual-list.vue

<template>
  <div
    class="view"
    :ref="el => viewRef = el"
    @scroll="handleScroll"
  >
    <div
      class="phantom"
      :style="{ height: `${itemSize * data.length}px` }"
    >
    </div>
    <div
      class="list"
      :style="{ transform: `translateY(${translateLen}px)` }"
    >
      <div
        v-for="item in visibleList"
        :style="{ height: `${itemSize}px` }"
      >
        {{ item }}
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed, ref } from 'vue';

const props = defineProps({
  data: {
    type: Array,
    default: [],
  },
  itemSize: {
    type: Number,
    default: 32,
  }
})

const translateLen = ref(0);

const viewRef = ref(null);

const start = ref(0);
const visibleCount = computed(() => Math.ceil((viewRef.value?.clientHeight ?? 0) / props.itemSize));
const visibleList = computed(() => props.data.slice(start.value, start.value + visibleCount.value));

const handleScroll = () => {
  const scrollTop = viewRef.value.scrollTop;
  start.value = Math.floor(scrollTop / props.itemSize);

  translateLen.value = scrollTop - (scrollTop % props.itemSize);
}

</script>

<style scoped>
.view {
  position: relative;
  height: 100%;
  overflow: auto;
}

.phantom {
  position: absolute;
  width: 100%;
}

.list {
  position: absolute;
}
</style>

tip

  • item的高度保持一致
  • phantom用于顯示一致的滾動(dòng)條
  • list部分通過(guò)translate顯示在視區(qū)內(nèi)

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

相關(guān)文章

最新評(píng)論

延边| 安阳市| 文山县| 北票市| 昭觉县| 敦化市| 白城市| 庆城县| 潼南县| 包头市| 景泰县| 商河县| 潮安县| 温泉县| 广丰县| 屏东县| 鱼台县| 额敏县| 衡水市| 铁岭市| 阳朔县| 察隅县| 凤阳县| 南漳县| 古蔺县| 新竹县| 巴林左旗| 巴中市| 南岸区| 盐津县| 辽宁省| 柯坪县| 巴楚县| 大关县| 德庆县| 措勤县| 女性| 东丰县| 新郑市| 即墨市| 湖口县|