Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vue实现虚拟列表(固定子项高度) #55

Open
varHarrie opened this issue Oct 11, 2023 · 0 comments
Open

Vue实现虚拟列表(固定子项高度) #55

varHarrie opened this issue Oct 11, 2023 · 0 comments

Comments

@varHarrie
Copy link
Owner

<template>
  <div class="list" ref="listRef" @scroll="onScroll">
   <div class="inner" :style="{ height: innerHeight }"></div>
   <div class="item" v-for="item of visibleList" :style="{ top: item.top }">{{ item.value }}</div>
  </div>
</template>

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

const itemHeight = 50;
const bufferSize = 2;
const list = Array.from({length: 10000}).map((_, i) => i);

const listRef = ref();
const listMeta = reactive({ offsetHeight: 0, scrollTop: 0 });

const onScroll = () => {
  listMeta.offsetHeight = listRef.value.offsetHeight;
  listMeta.scrollTop = listRef.value.scrollTop;
}

onMounted(() => {
  if (listRef.value) onScroll();
})

const innerHeight = computed(() => itemHeight * list.length + 'px');

const visibleList = computed(() => {
  const result = [];
  const { scrollTop, offsetHeight } = listMeta;

  const startIndex = Math.floor(scrollTop / itemHeight);
  const endIndex = Math.ceil((scrollTop + offsetHeight) / itemHeight);

  const finalStartIndex = Math.max(0, startIndex - bufferSize);
  const finalEndIndex = Math.min(list.length - 1,  endIndex + bufferSize);

  for (let i = finalStartIndex; i < finalEndIndex; i++) {
    result.push({
      top: i * itemHeight + 'px',
      value: list[i]
    })
  }

  return result;
});
</script>

<style>
.list {
  position: relative;
  height: 500px;
  width: 300px;
  border: 2px solid #000;
  overflow-y: scroll;

  .item {
    position: absolute;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    box-sizing: border-box;
    height: 50px;
    border: 1px solid #000;
  }
}
</style>
@varHarrie varHarrie added this to the Snippets milestone Oct 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant