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

vue實現(xiàn)列表滑動下拉加載數(shù)據(jù)的方法

 更新時間:2024年11月25日 14:10:51   作者:在路上`  
文章介紹了如何使用Vue實現(xiàn)列表滑動下拉加載數(shù)據(jù)的功能,通過監(jiān)聽滾動事件,檢測用戶是否滾動到底部,然后動態(tài)加載更多數(shù)據(jù),附帶了實現(xiàn)思路和案例代碼,感興趣的朋友一起看看吧

一、實現(xiàn)效果

二、實現(xiàn)思路

使用滾動事件監(jiān)聽器來檢測用戶是否滾動到底部,然后加載更多數(shù)據(jù)

  • 監(jiān)聽滾動事件。
  • 檢測用戶是否滾動到底部。
  • 加載更多數(shù)據(jù)。

三、案例代碼

<div class="drawer-content">
  <div ref="loadMoreTrigger" class="load-more-trigger"></div>
    <div v-if="isLoading" class="loading-button">
      <button type="primary" loading>加載中...</button>
    </div>
</div>
import { onBeforeUnmount, onMounted, ref, watch } from 'vue';
const isLoading = ref(false);
const page = ref(1);
const loadMoreData = async () => {
  if (isLoading.value) return;
  isLoading.value = true;
  // 模擬異步加載數(shù)據(jù)
  setTimeout(() => {
    const newItems: any = props.chartTableData
      ?.slice(0, 10)
      .map((item) => ({ ...item, id: item.id + page.value * 100 }));
    cardList.value = [...cardList.value, ...newItems];
    page.value += 1;
    isLoading.value = false;
  }, 1000);
};
const handleScroll = () => {
  const drawerContent = document.querySelector('.drawer-content');
  if (drawerContent) {
    const { scrollTop, scrollHeight, clientHeight } = drawerContent;
    if (scrollTop + clientHeight >= scrollHeight - 10) {
      loadMoreData();
    }
  }
};
onMounted(() => {
  const drawerContent = document.querySelector('.drawer-content');
  if (drawerContent) {
    drawerContent.addEventListener('scroll', handleScroll);
  }
});
onBeforeUnmount(() => {
  const drawerContent = document.querySelector('.drawer-content');
  if (drawerContent) {
    drawerContent.removeEventListener('scroll', handleScroll);
  }
});
.load-more-trigger {
  height: 1px;
}
.loading-button {
  text-align: center;
  margin-top: 10px;
}

到此這篇關(guān)于vue實現(xiàn)列表滑動下拉加載數(shù)據(jù)的文章就介紹到這了,更多相關(guān)vue下拉加載數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論