使用swiper的loop屬性導(dǎo)致復(fù)制多個(gè)數(shù)據(jù)的問(wèn)題

1.問(wèn)題描述

使用swiper,slides-per-view 設(shè)置為10,頁(yè)面上要展示10條數(shù)據(jù)
我使用的Swiper版本時(shí)是8.4.5的版本

  <swiper
    :slides-per-view="10"
    :space-between="10"
    :speed="1000"
    :loop="true"
    :autoplay="{
      delay: 0,
      disableOnInteraction: true
    }"
    :modules="modules"
    direction="vertical">
    <swiper-slide v-for="(record, index) in props.swiperData" :key="index">
      ... 需要展示的代碼
    </swiper-slide>
  </swiper>
image.png

2.分析

如圖,當(dāng)時(shí)數(shù)據(jù)有2條,
當(dāng)數(shù)據(jù)小于我們配置的展示10條數(shù)據(jù),因此loop 會(huì)自動(dòng)復(fù)制數(shù)據(jù)填充。 如果有10條數(shù)據(jù),在輪播時(shí),會(huì)不斷的復(fù)制最后幾條數(shù)據(jù),達(dá)到無(wú)限loop的效果
知道了loop的原理,對(duì)于多余的數(shù)據(jù),我們就有解決方案了

3.解決方案

我們可以根據(jù)后端返回?cái)?shù)據(jù)的長(zhǎng)度去做判斷,如果小于10,則不進(jìn)行l(wèi)oop,否則進(jìn)行l(wèi)oop

:loop="props.swiperData.length >10 "
 // 或者使用計(jì)算屬性
 const isLoop = computed(() => props.swiperData.length >= 10)
 :loop="isLoop"

可能存在得問(wèn)題
使用以上方法,可能會(huì)出現(xiàn)isLoop為true但是swiper不復(fù)制得問(wèn)題,這是因?yàn)閟wiper初始化時(shí),loop計(jì)算為false,后面通過(guò)計(jì)算屬性計(jì)算后,isLoop為true,swiper未更新loop得狀態(tài),這是swiper得一個(gè)bug
方案
目前采用得方案是 通過(guò)v-if 得方式

<swiper v-if="swiperData.length">
  </swiper>

如果有更好的方式,歡迎在評(píng)論區(qū)留言

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容