vue3 使用swiper7不能自动轮播

记录一次坑~

前言

swiper已经是到8了,但是目前还是在发展更新中,建议vue3的朋友们还是先用swiper7
安装的时候记得要加版本号npm install swiper@7.4.1
因为现在直接npm install swiper下载下来的是8.0.7的版本了
以下可以查看swiper的以往版本的对比数据,附上官网链接swiper各版本对比
在这里插入图片描述
想看各版本稳定版的也满足你们~
在这里插入图片描述

不能自动轮播

按照官网给的例子,然后加上 :autoplay="{ delay: 2500, disableOnInteraction: false }"

<template>
  <swiper :slides-per-view="1" :autoplay="{ delay: 2500, disableOnInteraction: false }" :loop="true" :space-between="50">
    <swiper-slide>Slide 1</swiper-slide>
    <swiper-slide>Slide 2</swiper-slide>
    <swiper-slide>Slide 3</swiper-slide>
    <swiper-slide>Slide 4</swiper-slide>
    <swiper-slide>Slide 5</swiper-slide>
    <swiper-slide>Slide 6</swiper-slide>
  </swiper>
</template>
<script lang="ts">
// Import Swiper Vue.js components
import { Swiper, SwiperSlide } from 'swiper/vue'

// Import Swiper styles
import 'swiper/css'
export default {
  components: {
    Swiper,
    SwiperSlide
  },
  setup() {
    return {
    }
  }
}
</script>

原以为能见到自动轮播,但是事实上毫无反应
还需要引入autoPlay这个模块!!!
swiper现在都模块化成这样了吗???
在这里插入图片描述
附上完整代码

<template>
  <swiper :slides-per-view="1" :autoplay="{ delay: 2500, disableOnInteraction: false }" :loop="true" :space-between="50" :modules="modules">
    <swiper-slide>Slide 1</swiper-slide>
    <swiper-slide>Slide 2</swiper-slide>
    <swiper-slide>Slide 3</swiper-slide>
    <swiper-slide>Slide 4</swiper-slide>
    <swiper-slide>Slide 5</swiper-slide>
    <swiper-slide>Slide 6</swiper-slide>
  </swiper>
</template>
<script lang="ts">
// Import Swiper Vue.js components
import { Swiper, SwiperSlide } from 'swiper/vue'
import { Autoplay } from 'swiper'

// Import Swiper styles
import 'swiper/css'
export default {
  components: {
    Swiper,
    SwiperSlide
  },
  setup() {
    return {
      modules: [Autoplay]
    }
  }
}
</script>

以后使用swiper7可得好好查看一下以下的模块了,分页、导航、滚动条、自动轮播、网格、淡入淡出等等
在这里插入图片描述
最后凡事还是得看官网swiper

完结啦!!!

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
对于在Vue 3中使用swiper插件,并且在鼠标悬停时暂停轮播的需求,以下是一个示例的Vue 3 TypeScript写法: 首先,你需要安装swiper插件。可以使用npm或yarn命令来安装: ```bash npm install swiper # 或者 yarn add swiper ``` 接下来,在你的Vue组件中,可以按照以下方式使用swiper: ```vue <template> <div class="swiper-container" @mouseenter="stopSwiper" @mouseleave="startSwiper"> <div class="swiper-wrapper"> <div class="swiper-slide">Slide 1</div> <div class="swiper-slide">Slide 2</div> <div class="swiper-slide">Slide 3</div> </div> </div> </template> <script lang="ts"> import { ref, onMounted, onUnmounted } from 'vue'; import Swiper, { Autoplay } from 'swiper'; // 引入swiper的样式(如果你没有自定义样式) import 'swiper/swiper-bundle.css'; export default { setup() { const swiperRef = ref<HTMLElement | null>(null); let swiperInstance: Swiper | null = null; const stopSwiper = () => { if (swiperInstance) { swiperInstance.autoplay.stop(); } }; const startSwiper = () => { if (swiperInstance) { swiperInstance.autoplay.start(); } }; onMounted(() => { swiperInstance = new Swiper(swiperRef.value, { // swiper的配置选项 autoplay: { delay: 3000, // 自动切换的时间间隔 }, // 其他配置项... }); }); onUnmounted(() => { if (swiperInstance) { swiperInstance.destroy(); swiperInstance = null; } }); return { swiperRef, stopSwiper, startSwiper, }; }, }; </script> <style> /* 自定义样式 */ .swiper-container { width: 100%; height: 300px; } </style> ``` 这个示例中,我们在组件的`<template>`部分使用swiper的HTML结构。在`<script>`部分,我们使用Vue 3的Composition API来编写逻辑。 在`setup()`函数中,我们使用了`ref`来创建一个`swiperRef`引用,用于获取swiper容器的DOM元素。我们还定义了一个`swiperInstance`变量来持有swiper的实例。 在`stopSwiper`和`startSwiper`函数中,我们分别调用了swiper实例的`autoplay.stop()`和`autoplay.start()`方法来暂停和恢复轮播。 在`onMounted`钩子函数中,我们创建了swiper实例,并将其赋值给`swiperInstance`变量。你可以根据需要配置swiper的选项。 在`onUnmounted`钩子函数中,我们在组件销毁时销毁swiper实例。 最后,我们通过`return`语句将需要在模板中使用的变量和方法暴露出去。 请注意,这只是一个基本示例,你可能需要根据自己的需求进行适当的修改和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小邓不爱吃芹菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值