vue轮播图插件之vue-awesome-swiper

本文详细介绍了在移动端项目中使用vue-awesome-swiper插件实现触摸滑动轮播图的方法,包括插件安装、全局及组件引入、配置参数详解,以及解决常见问题如自动滚动、索引同步和小圆点显示等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

移动端轮播图插件,在使用iview图形界面插件中的carousel组件无法实现触摸滑动后,转而使用vue-awesome-swiper插件

1.npm安装

npm i vue-awesome-swiper -S
  • 我这里安装的是下面的这个版本

2.使用

  • 全局导入:
import Vue from 'vue'
import vueSwiper from 'vue-awesome-swiper'
/* 样式的话,我这里有用到分页器,就在全局中引入了样式 */
import 'swiper/dist/css/swiper.css'
Vue.use(vueSwiper);
  • 组件引入
import { swiper, swiperSlide } from "vue-awesome-swiper";
require("swiper/dist/css/swiper.css");
components: {
  swiper,
  swiperSlide
},
  • 在template中使用
<swiper :options="swiperOption" class="swiper-wrap"  ref="mySwiper" v-if="banner.length!=0">
  <swiper-slide v-for="(item,index) in banner" :key="index" >
    <img :src="item.image" alt="" />
  </swiper-slide>
  <!-- 常见的小圆点 -->
  <div class="swiper-pagination"  v-for="(item,index) in banner" :key="index" slot="pagination" ></div>
</swiper>
<!-- 显示数字 -->
<div class="number">{{imgIndex}}/{{detailimages.length}}</div>

  • data中配置
data() {
    const that = this;
    return {
      imgIndex: 1,
      swiperOption: {
        //是一个组件自有属性,如果notNextTick设置为true,组件则不会通过NextTick来实例化swiper,也就意味着你可以在第一时间获取到swiper对象,假如你需要刚加载遍使用获取swiper对象来做什么事,那么这个属性一定要是true
        notNextTick: true,
        //循环
        loop: true,
        //设定初始化时slide的索引
        initialSlide: 0,
        //自动播放
        autoplay: {
          delay: 1500,
          stopOnLastSlide: false,
          /* 触摸滑动后是否继续轮播 */
          disableOnInteraction: false
        },
        //滑动速度
        speed: 800,
        //滑动方向
        direction: "horizontal",
        //小手掌抓取滑动
        grabCursor: true,
        on: {
          //滑动之后回调函数
          slideChangeTransitionStart: function() {
            /* realIndex为滚动到当前的slide索引值 */
            that.imgIndex= this.realIndex - 1;
          },
        },
        //分页器设置
        pagination: {
          el: ".swiper-pagination",
          clickable: true,
          type: "bullets"
        }
      }
   };
},

3.遇见的问题

  • 这个插件,在图片只有一张时,仍然会自动滚动
这里很需要注意的一点就是,如果你直接设置autoplay为true的话,在你触摸滑动图片后,他就不会再自动滚动了
/* 这里我是在使用接口请求后,对返回的数据进行判断 */
created() {
  this.$Request({
    url: '',
    method: 'get',
    success: res => {
      this.swiperOption.autoplay = res.result.data.length != 1 ? {
        delay: 1500,
        stopOnLastSlide: false,
        disableOnInteraction: false
        } : false;
     }
  })
}
  • 在on里面,监听slideChangeTransitionStart方法时,在开始的时候,我用的是activeIndex来设置对应的索引值,这个的话,滑向下一张没有发现问题,后面,自己试着滑向上一张图片,就发现出现问题,这个值不对应了,使用realIndex
on: {
   slideChangeTransitionStart: function() {
      that.imgIndex = this.realIndex + 1;
   },
},
  • 在swiper这个容器中,会出现滚动到最后一张图片后就不自动滚动了,以及出现底边的小圆点写了后不显示的问题
原因是因为this.commodity刚开始数据为[],后来赋值才有值,所以要先判断this.commodity是否为空,这里就在swiper这个容器中进行判断,若数据长度为0,就不显示这个容器
<swiper class='swiImgs' :options="swiperOption" v-if="commodity.length!=0"></swiper>
### 使用 `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 'vue'; import 'swiper/css/swiper.css'; const props = defineProps({ slides: { type: Array, default() { return ['Slide 1', 'Slide 2', 'Slide 3']; } }, }); let mySwiper; onMounted(() => { import('vue-awesome-swiper').then(({ Swiper }) => { mySwiper = new Swiper('.swiper-container', { loop: true, // 如果需要分页器 pagination: { el: '.swiper-pagination', }, // 如果需要前进后退按钮 navigation: { nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev', }, }); }); }); </script> <style scoped> /* Add custom styles here */ .swiper-slide { text-align: center; font-size: 18px; background-color: #fff; } </style> ``` 此示例展示了如何利用 `vue-awesome-swiper` 结合 Vue 3 构建一个基本的轮播图,并包含了分页指示符和导航箭头的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值