uni-app swiper數(shù)量過多時卡頓優(yōu)化方案,微信小程序swiper優(yōu)化

問題:

swiper數(shù)量達到大約400+時候會出現(xiàn)明顯滑動卡頓呵哨,渲染慢的問題赁濒,達到1000+時候需要幾十秒時間,或者可能導致渲染失敗孟害。

解決方案:

配置circular="true"屬性開啟銜接滑動拒炎,即播放到末尾后重新回到開頭。然后固定用于展示的swiper-item只設置3個挨务,當滑動時候去替換展示的數(shù)據(jù)击你。這種方法可以展示幾千萬的數(shù)據(jù)展示都沒問題。

//頁面源碼

<template>
  <view class="content">
    <view class="title">{{originIndex +1 }}/{{ originList.length }}</view>
    <swiper class="swiper" circular @change="swiperChange" swiperDuration="250">
      <swiper-item v-for="(item, index) in displaySwiperList" :key="index">
        <view class="wrap_content">{{ item }} </view>
      </swiper-item>
    </swiper>
  </view>
</template>

<script>
export default {
  data() {
    return {
      originList: [], // 源數(shù)據(jù)
      displaySwiperList: [], // swiper需要的數(shù)據(jù)
      displayIndex: 0, // 用于顯示swiper的真正的下標數(shù)值只有:0谎柄,1丁侄,2。
      originIndex: 0, // 記錄源數(shù)據(jù)的下標
    };
  },
  methods: {
    /**
     * 初始一個顯示的swiper數(shù)據(jù)
     * @originIndex  從源數(shù)據(jù)的哪個開始顯示默認0朝巫,如從其他頁面跳轉(zhuǎn)進來鸿摇,要顯示第n個,這個參數(shù)就是他的下標
     */
    initSwiperData(originIndex = this.originIndex) {
      const originListLength = this.originList.length; // 源數(shù)據(jù)長度
      const displayList = [];
      displayList[this.displayIndex] = this.originList[originIndex];
      displayList[this.displayIndex - 1 == -1 ? 2 : this.displayIndex - 1] =
        this.originList[
          originIndex - 1 == -1 ? originListLength - 1 : originIndex - 1
        ];
      displayList[this.displayIndex + 1 == 3 ? 0 : this.displayIndex + 1] =
        this.originList[
          originIndex + 1 == originListLength ? 0 : originIndex + 1
        ];
      this.displaySwiperList = displayList;
    },
    /**
     * swiper滑動時候
     */
    swiperChange(event) {
      const { current } = event.detail;
      const originListLength = this.originList.length; // 源數(shù)據(jù)長度
      // =============向后==========
      if (this.displayIndex - current == 2 || this.displayIndex - current == -1) {
        this.originIndex =
          this.originIndex + 1 == originListLength ? 0 : this.originIndex + 1;
        this.displayIndex = this.displayIndex + 1 == 3 ? 0 : this.displayIndex + 1;
        this.initSwiperData(this.originIndex);
      }
      // ======如果兩者的差為-2或者1則是向前滑動============
      else if (this.displayIndex - current == -2 || this.displayIndex - current == 1) {
        this.originIndex = this.originIndex - 1 == -1 ? originListLength - 1 : this.originIndex - 1;
        this.displayIndex = this.displayIndex - 1 == -1 ? 2 : this.displayIndex - 1;
        this.initSwiperData(this.originIndex);
      }
    },
  },
  created() {
    for (let i = 1; i <= 1300; i++) {
      this.originList.push(i);
    }
    this.initSwiperData();
  },
};
</script>

<style>
.title {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 60rpx;
}
.swiper {
  height: calc(100vh - 120rpx);
}
.wrap_content {
  border-radius: 20rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  background: gray;
  height: 100vh;
  color: aqua;
  font-size: 80px;
  margin: 0rpx 40rpx;
}
</style>

注意:

1劈猿、swiper-item的key一定要設置拙吉,并且用index

<swiper-item v-for="(item, index) in displaySwiperList" :key="index">
        <view class="wrap_content">{{ item }} </view>
</swiper-item>

2揪荣、如果只有一張情況筷黔,不想讓它來回滾動≌叹保可以設置circular佛舱,但是circular無法直接動態(tài)設置,我們可以使用computed來設置

<template>
  <swiper :circular="!canCircular" >  </swiper>
</template>

export default {
  data() {
    return {
      originList:[]
    }  
  },
  computed: {
    canCircular() {
      return this.originList.length > 0;   // 看這里重點
    }
  }
}

gitlab地址:

https://gitee.com/xiaoguoyao/uni-app

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末挨决,一起剝皮案震驚了整個濱河市请祖,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌凰棉,老刑警劉巖损拢,帶你破解...
    沈念sama閱讀 221,548評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異撒犀,居然都是意外死亡福压,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評論 3 399
  • 文/潘曉璐 我一進店門或舞,熙熙樓的掌柜王于貴愁眉苦臉地迎上來荆姆,“玉大人,你說我怎么就攤上這事映凳〉ㄍ玻” “怎么了?”我有些...
    開封第一講書人閱讀 167,990評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長仆救。 經(jīng)常有香客問我抒和,道長,這世上最難降的妖魔是什么彤蔽? 我笑而不...
    開封第一講書人閱讀 59,618評論 1 296
  • 正文 為了忘掉前任摧莽,我火速辦了婚禮,結(jié)果婚禮上顿痪,老公的妹妹穿的比我還像新娘镊辕。我一直安慰自己,他們只是感情好蚁袭,可當我...
    茶點故事閱讀 68,618評論 6 397
  • 文/花漫 我一把揭開白布征懈。 她就那樣靜靜地躺著,像睡著了一般揩悄。 火紅的嫁衣襯著肌膚如雪卖哎。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,246評論 1 308
  • 那天虏束,我揣著相機與錄音棉饶,去河邊找鬼。 笑死镇匀,一個胖子當著我的面吹牛照藻,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播汗侵,決...
    沈念sama閱讀 40,819評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼幸缕,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了晰韵?” 一聲冷哼從身側(cè)響起发乔,我...
    開封第一講書人閱讀 39,725評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎雪猪,沒想到半個月后栏尚,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,268評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡只恨,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,356評論 3 340
  • 正文 我和宋清朗相戀三年译仗,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片官觅。...
    茶點故事閱讀 40,488評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡纵菌,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出休涤,到底是詐尸還是另有隱情咱圆,我是刑警寧澤,帶...
    沈念sama閱讀 36,181評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站序苏,受9級特大地震影響手幢,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜杠览,卻給世界環(huán)境...
    茶點故事閱讀 41,862評論 3 333
  • 文/蒙蒙 一弯菊、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧踱阿,春花似錦、人聲如沸钦铁。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽牛曹。三九已至佛点,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間黎比,已是汗流浹背超营。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留阅虫,地道東北人演闭。 一個月前我還...
    沈念sama閱讀 48,897評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像颓帝,于是被迫代替她去往敵國和親米碰。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,500評論 2 359

推薦閱讀更多精彩內(nèi)容