• vue中使用swiper-slide时,循环轮播失效?


    前言

      vue 项目中使用时,组件swiper-slide 如果用v-for循环的话,loop:true 就不能无缝轮播,每次轮播到最后一张就停止了???

    正文

      代码如下:  

     <swiper :options="swiperOption2">
        <swiper-slide v-for="(item, index) of showProduct" :key="index">
          <el-row>
            <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
              <div class="product-div">
                <div class="product-p" v-html='item.introduction'></div>
              </div>
            </el-col>
            <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
              <div id="product-img" v-if="item.androidcode !== null && item.androidcode !== ''">
                <div class="grid-img">
                  <img class="android-img " :src="httpImgSrc+'/showImg@file.do?id=' + item.androidcode">
                </div>
              </div>
            </el-col>
          </el-row>
        </swiper-slide>
        <div v-show="isShow" class="swiper-button-prev" id="swiper-button-prev" slot="button-prev" style="mergin-top:-50px"></div>
        <div v-show="isShow" class="swiper-button-next" id="swiper-button-next" slot="button-next"></div>
        <div class="swiper-pagination-1" slot="pagination" id="swiper-pagination1"></div>
      </swiper>

    data数据:

    swiperOption2: {
            slidesPerView: 1,
            spaceBetween: 30,
            centeredSlides: true,
            slidesPerGroup: 1,
            loop: true,
            pagination: {
              el: '.swiper-pagination-1',
              clickable: true
            },
            navigation: {
              nextEl: '.swiper-button-next',
              prevEl: '.swiper-button-prev'
            }
          }

    这么写看似是没毛病的。可是loop:true这个条件就失效了。这是为啥呢?

    仔细查看swiper文档。

      loop

      设置为true 则开启loop模式。loop模式:会在原本slide前后复制若干个slide(默认一个)并在合适的时候切换,让Swiper看起来是循环的。 
    loop模式在与free模式同用时会产生抖动,因为free模式下没有复制slide的时间点。

    注意红字,在原本基础上复制若干个slide,可是在vue的v-for中时,异步加载的数据都还没有返回时,就先加载了Swiper组件并复制了sliper

    解决方法;

      利用v-if的属性,v-if=showProduct.length,确保异步加载的数据已经返回后,再加载swiper组件

    代码如下:

      

    <swiper :options="swiperOption2"  v-if="showProduct.length">
        <swiper-slide v-for="(item, index) of showProduct" :key="index">
          <el-row>
            <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
              <div class="product-div">
                <div class="product-p" v-html='item.introduction'></div>
              </div>
            </el-col>
            <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
              <div id="product-img" v-if="item.androidcode !== null && item.androidcode !== ''">
                <div class="grid-img">
                  <img class="android-img " :src="httpImgSrc+'/showImg@file.do?id=' + item.androidcode">
                </div>
              </div>
            </el-col>
          </el-row>
        </swiper-slide>
        <div v-show="isShow" class="swiper-button-prev" id="swiper-button-prev" slot="button-prev" style="mergin-top:-50px"></div>
        <div v-show="isShow" class="swiper-button-next" id="swiper-button-next" slot="button-next"></div>
        <div class="swiper-pagination-1" slot="pagination" id="swiper-pagination1"></div>
      </swiper>

     

    <div  v-if="showProduct.length">
    <swiper :options="swiperOption2" >
        <swiper-slide v-for="(item, index) of showProduct" :key="index">
          <el-row>
            <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
              <div class="product-div">
                <div class="product-p" v-html='item.introduction'></div>
              </div>
            </el-col>
            <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
              <div id="product-img" v-if="item.androidcode !== null && item.androidcode !== ''">
                <div class="grid-img">
                  <img class="android-img " :src="httpImgSrc+'/showImg@file.do?id=' + item.androidcode">
                </div>
              </div>
            </el-col>
          </el-row>
        </swiper-slide>
        <div v-show="isShow" class="swiper-button-prev" id="swiper-button-prev" slot="button-prev" style="mergin-top:-50px"></div>
        <div v-show="isShow" class="swiper-button-next" id="swiper-button-next" slot="button-next"></div>
        <div class="swiper-pagination-1" slot="pagination" id="swiper-pagination1"></div>
      </swiper>
    </div>

    重启项目,loop完美解决

  • 相关阅读:
    python中的 if __name__ == “__main__”: 有什么用
    LeetCode Two Sum 解题思路(python)
    numpy cheat sheet
    matlab中换行
    从github下载一个单一文件
    tensorflow轮子下载地址 wheels(whl)
    tensorflow报错 Key Conv/biases not found in checkpoint
    tensorflow报错 tensorflow Resource exhausted: OOM when allocating tensor with shape
    西门子 1500 1200 PLC,位访问, 字节访问
    查看pip install安装的python包的位置
  • 原文地址:https://www.cnblogs.com/clwydjgs/p/10313574.html
Copyright © 2020-2023  润新知