轮播图|vue 使用 swiper 的坑和方法步骤

本文档展示了如何在Vue项目中安装和使用vue-awesome-swiper组件来创建轮播图。通过npm安装指定版本的vue-awesome-swiper和swiper依赖,然后在组件中局部引入并配置样式及选项,实现了一个包含多个卡片信息的轮播效果。每个卡片包含统计数据和详细信息,并提供了分页和导航箭头功能。
  • 安装依赖

安装vue-awesome-swipe:

npm i vue-awesome-swiper@3.1.3 -S

安装swiper依赖

 npm install swiper

注意:ue-awesome-swipe要注意安装的版本,3版本的比较不踩雷

  • 局部引入组件(因为只有一个页面使用到了这个)
import { swiper, swiperSlide } from 'vue-awesome-swiper';
import 'swiper/swiper-bundle.css';
  • 使用组件

HTML的代码

<div class="recommendPage">
            <swiper :options="swiperOption" ref="mySwiper">
              <swiper-slide v-for="item in slideOptions" :key="item.name">
                    <a-card
                      class="cardClass"
                      hoverable
                      style="height: 260px"
                      @click="toDetail()"
                    >
                    <a-row>
                      <a-col :span="6">
                        <a-icon type="mail" theme="twoTone" :style="{fontSize:'60px'}"/>
                      </a-col>
                      <a-col :span="18">
                        <span :style="{fontWeight:'bold',fontSize:'16px'}">{{item.name}}</span>
                        <a-row :style="{marginTop:'20px'}">
                          <a-col :span="12">
                          <a-statistic
                          :value-style="{ color: 'rgb(82, 192, 254)' }"
                          title="接入总条数" :value="item.value" />
                          </a-col>
                          <a-col :span="12">
                            <a-statistic
                            title="最新日期接入总条数"
                            :value-style="{ color: 'rgb(82, 192, 254)' }"
                            :precision="2" :value="112893"/>
                          </a-col>
                        </a-row>
                        <a-row>
                          <a-col :span="12">
                          <a-statistic
                          title="最早数据日期"
                          :value-style="{ color: 'rgb(82, 192, 254)' }"
                          :value="112893" />
                          </a-col>
                          <a-col :span="12">
                            <a-statistic
                          :value-style="{ color: 'rgb(82, 192, 254)' }"
                          title="最新数据日期"
                          :precision="2" :value="112893"/>
                          </a-col>
                        </a-row>
                      </a-col>
                    </a-row>
                    <template :style="{height:'30px'}" #actions>
                      <a-row>
                        <a-col :span="12"><span>奇安信</span></a-col>
                        <a-col :span="12"><span>份数据</span></a-col>
                      </a-row>
                    </template>
                    </a-card>
              </swiper-slide>
              <div class="swiper-pagination" slot="pagination"></div>
              <div class="swiper-button-prev" slot="button-prev"></div>
              <div
              class="swiper-button-next"
              slot="button-next"></div>
            </swiper>
          </div>

配置需要的option项(写在了data中)

这个根据自己需要的内容去写,可以参考官网给出的例子和api去配置参数

swiperOption: {
        // loop: true, // 循环往复的方式
        // 展示三个
        slidesPerView: 3,
        slidesPerGroup: 3,
        // slidesOffsetBefore: 0,
        loopFillGroupWithBlank: true,
        // 宽度
        width: 1450,
        spaceBetween: 50,

        // autoplay: {
        //   delay: 3000,
        //   stopOnLastSlide: false,
        //   disableOnInteraction: false,
        // },
        // 显示分页
        // pagination: {
        //   el: '.swiper-pagination',
        //   clickable: true,
        // },
        // 设置点击箭头
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev',
        },
      },
  • 配置style的参数(根据需要去设置)
.cardClass{
  box-shadow: 0 0 20px #eee;
}
.recommendPage .swiper-container{
  position: relative;
  width: 100%;
  height: 200px;
  background: #fff;
}
.recommendPage{
  width: '100%';
}
.recommendPage .swiper-container .swiper-slide{
  width: 100%;
  line-height: 200px;
  background: #fff;
  color: #000;
  font-size: 16px;
  text-align: center;
}
  • 最终效果:

<think>好的,我现在需要解决用户的问题:在Vue使用Swiper 2.7.6实现轮播图并获取当前显示图片的下标。首先,我需要回忆一下SwiperVue中的使用方法,尤其是较旧的版本如2.7.6。用户提到的是Swiper 2.7.6,这可能对应的是vue-awesome-swiper的某个特定版本,需要确认版本兼容性。 根据用户提供的引用内容,引用[4]提到安装指定版本3的vue-awesome-swiper,而高版本可能有兼容问题。但用户需要的是Swiper 2.7.6,可能需要查找对应的vue-awesome-swiper版本。不过,可能用户混淆了Swiper版本vue-awesome-swiper的版本,需要理清两者的关系。通常,vue-awesome-swiper的版本Swiper本身的版本是相关联的,比如vue-awesome-swiper@3.x对应Swiper 4.x,而更老的版本可能对应Swiper 3.x或更低。用户提到的Swiper 2.7.6可能比较古老,可能需要安装特定的旧版vue-awesome-swiper。 接下来,用户需要获取当前显示图片的下标。Swiper的API中有事件可以监听,比如slideChange事件,当轮播图切换时会触发,可以通过这个事件获取当前活动的索引。在Vue组件中,需要正确绑定这些事件,并在回调函数中更新数据。 我需要确保示例代码中的版本安装正确,比如使用npm安装指定版本的vue-awesome-swiperSwiper。可能需要查阅旧版文档,确认事件属性的使用方式是否正确。例如,旧版Swiper可能使用onSlideChangeEnd事件而不是slideChange。 另外,要注意在Vue组件中正确引入Swiper样式文件,并在mounted生命周期钩子中初始化Swiper实例,因为DOM元素需要已经渲染完成才能正确初始化。同时,使用v-for循环生成轮播项,并确保每个项的key正确。 最后,确保示例代码能够正确获取当前活动索引,并通过Vue的数据绑定显示在页面上。可能还需要处理自动播放、循环等配置选项,但用户的主要需求是获取下标,所以示例应重点展示这部分。</think>在Vue使用Swiper 2.7.6实现轮播图并获取当前下标,需注意版本兼容性事件监听方式。以下是具体实现步骤: --- ### 一、安装依赖 ```bash # 安装指定版本(对应Swiper 2.x) npm install vue-awesome-swiper@2.7.6 --save ``` --- ### 二、基础实现与下标获取 ```vue <template> <div> <!-- 轮播容器 --> <swiper :options="swiperOption" @slideChange="onSlideChange"> <!-- 轮播项 --> <swiper-slide v-for="(item, index) in list" :key="index"> <img :src="item.img"> </swiper-slide> <!-- 分页器 --> <div class="swiper-pagination" slot="pagination"></div> </swiper> <!-- 显示当前下标 --> <div>当前显示的下标:{{ activeIndex }}</div> </div> </template> <script> import 'swiper/dist/css/swiper.min.css' import { swiper, swiperSlide } from 'vue-awesome-swiper' export default { components: { swiper, swiperSlide }, data() { return { activeIndex: 0, list: [ { img: 'image1.jpg' }, { img: 'image2.jpg' }, { img: 'image3.jpg' } ], swiperOption: { pagination: '.swiper-pagination', // 分页器 autoplay: 3000, // 自动播放 loop: true, // 循环模式 onSlideChangeEnd: (swiper) => { // 兼容旧版事件监听 this.activeIndex = swiper.realIndex } } } }, methods: { // 新版事件监听方式(需要与onSlideChangeEnd二选一) onSlideChange(swiper) { this.activeIndex = swiper.realIndex } } } </script> ``` --- ### 三、关键点说明 1. **版本匹配** `vue-awesome-swiper@2.7.6`需配合`swiper@2.7.6`使用,若未自动安装需手动安装: ```bash npm install swiper@2.7.6 --save ``` 2. **下标获取原理** - 使用`realIndex`而非`activeIndex`(循环模式下更准确) - 旧版通过`onSlideChangeEnd`回调获取[^4] - 或通过`@slideChange`事件绑定方法获取 3. **循环模式处理** 当开启`loop: true`时,DOM结构会增加克隆节点,`realIndex`能正确映射原始数据下标。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值