【Swiper 】Vue3 中 Swiper 插件 loop 无限滚动、并且暂停的问题

上午把官网的合作伙伴做了,好坑,swiper 自动滚动展示的数量 slides-per-view 的两倍必须小于等于 *SwiperSlide* 组件的渲染数量,才能进行自动滚动,官网居然都没有说。
比如 slidesPerView = 6,那么 SwiperSlide 组件渲染的数量必须大于等于 6 的 2 倍,否则无法自动无限滚动!

    <Swiper
          @swiper="onSwiper"
          :autoplay="{ delay: 0, disableOnInteraction: false, stopOnLastSlide: false }"
          :modules="[Autoplay]"
          :slides-per-view="6"
          :loop="true"
          :speed="1500"
        >
          <SwiperSlide v-for="(item, index) in 12" :key="index" :virtualIndex="index">
            <div class="partner-card">
              <img v-if="index < 8" :src="`partner/1-${index + 1}.png`" alt="" />
              <img v-else :src="`partner/1-${index + 1 - 5}.png`" alt="" />
            </div>
            <div class="partner-card">
              <img v-if="index < 8" :src="`partner/2-${index + 1}.png`" alt="" />
              <img v-else :src="`partner/2-${index + 1 - 5}.png`" alt="" />
            </div>
            <div class="partner-card">
              <img v-if="index < 8" :src="`partner/3-${index + 1}.png`" alt="" />
              <img v-else :src="`partner/3-${index + 1 - 5}.png`" alt="" />
            </div>
            <div class="partner-card">
              <img v-if="index < 8" :src="`partner/4-${index + 1}.png`" alt="" />
              <img v-else :src="`partner/4-${index + 1 - 5}.png`" alt="" />
            </div>
          </SwiperSlide>
        </Swiper>
    :deep(.swiper-wrapper) {
      transition-timing-function: linear !important; /* 没错就是这个属性 */
    }

感谢

如何使Swiper的无限自动轮播更流畅,没有停顿感!
Swiper.js实现无缝滚动

### 使用 `vue-awesome-swiper` 插件Vue 3 中创建轮播图 为了在 Vue 3 中使用 `vue-awesome-swiper` 创建轮播图,需先安装必要的依赖项。由于 `vue-awesome-swiper` 主要基于 Swiper 库构建,因此也需要安装 Swiper。 #### 安装依赖包 通过 npm 或 yarn 来安装所需的库: ```bash npm install vue-awesome-swiper swiper --save ``` 或者如果偏好使用 yarn: ```bash yarn add vue-awesome-swiper swiper ``` #### 配置项目以支持 ES Modules 和 Composition API 考虑到 Vue 3 的特性以及对 Composition API 的支持,在 main.js 文件中引入并注册全局组件之前,应该确保按照官方文档配置好环境[^1]。 #### 编写轮播图组件代码 下面是一个简单的轮播图实现例子,适用于 Vue 3 及其组合式API风格: ```javascript // src/components/Carousel.vue <template> <div class="swiper-container"> <!-- Additional required wrapper --> <div class="swiper-wrapper"> <div v-for="(slide, index) in slides" :key="index" class="swiper-slide">{{ slide }}</div> </div> <!-- If we need pagination --> <div class="swiper-pagination"></div> <!-- If we need navigation buttons --> <div class="swiper-button-prev"></div> <div class="swiper-button-next"></div> </div> </template> <script setup> import { ref } from &#39;vue&#39;; import &#39;swiper/css/swiper.css&#39;; const props = defineProps({ slides: { type: Array, default() { return [&#39;Slide 1&#39;, &#39;Slide 2&#39;, &#39;Slide 3&#39;]; } }, }); let mySwiper; onMounted(() => { import(&#39;vue-awesome-swiper&#39;).then(({ Swiper }) => { mySwiper = new Swiper(&#39;.swiper-container&#39;, { loop: true, // 如果需要分页器 pagination: { el: &#39;.swiper-pagination&#39;, }, // 如果需要前进后退按钮 navigation: { nextEl: &#39;.swiper-button-next&#39;, prevEl: &#39;.swiper-button-prev&#39;, }, }); }); }); </script> <style scoped> /* Add custom styles here */ .swiper-slide { text-align: center; font-size: 18px; background-color: #fff; } </style> ``` 此示例展示了如何利用 `vue-awesome-swiper` 结合 Vue 3 构建一个基本的轮播图,并包含了分页指示符和导航箭头的功能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值