vue中使用swiper插件

vue中使用swiper插件,在pc端和h5端都可以使用。

使用版本

单个文件引入使用版本

"swiper": "^4.0.7",
"vue-awesome-swiper": "^3.1.3",

全局引入使用版本

"swiper": "^5.4.5",
"vue-awesome-swiper": "^4.1.1",

注意,这里版本如果对应不上,会产生错误。

安装

npm install swiper@4.0.7 vue-awesome-swiper@3.1.3 --save
npm install swiper@5.4.5 vue-awesome-swiper@4.1.1 --save

引入

单个文件里引入

import 'swiper/dist/css/swiper.css'
import { swiper,swiperSlide } from 'vue-awesome-swiper'
//在components注册
components: {swiper, swiperSlide},

在main.js中引入

/* 引入swiper */
import VueAwesomeSwiper from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'
Vue.use(VueAwesomeSwiper /* { default options with global component } */)

使用

在组件中应用,根据具体情况自行配置,具体参考swiper官方文档

html代码奉上:

<div class="swiper_box">
  <div class="prev">
    <img :src="leftBtn"
        alt=""
        class="swiper-button"
        @click="clickPrev">
  </div>
  <div class="swiper_">
    <swiper ref="mySwiper"
            :options="swiperOptions">
      <swiper-slide class="item-device"
                    v-for="(item, index) in deviceList"
                    :key="index">
        <div class="device">
          <div class="dev-img">
            <img :src="item.img"
                alt="">
          </div>
        </div>
      </swiper-slide>
    </swiper>
  </div>
  <div class="next">
    <img :src="rightBtn"
        alt=""
        class="swiper-button "
        @click="clickNext">
  </div>
</div>

js代码奉上:

swiperOptions: {
    slidesOffsetBefore: 10,
    slidesOffsetAfter: 10,
    slidesPerView: 'auto',
    freeMode: true,
    spaceBetween: 15,
    history: 'love',
    // 设置点击箭头
    navigation: {
      nextEl: '.next',
      prevEl: '.prev',
    },
 },
leftBtn: "",//左边点击按钮图片
rightBtn: ",//右边点击按钮图片
deviceList: require('./device.json').deviceList,//这是轮播图图片数组
computed: {
    swiper () {
      return this.$refs.mySwiper.$swiper
    }
},
methods: {
     /* 前一步 */
    clickPrev () {
      const that = this
      const length = that.deviceList.length
      if (that.activeBtn <= 0 || length == 0) {
        return false
      }
    },
    /* 后一步 */
    clickNext () {
      const that = this
      const length = that.deviceList.length
      if (that.activeBtn >= length - 1 || length == 0) {
        return false
      }
    },
}

css代码奉上:

<style lang="scss" scoped>
.swiper_box {
  width: 100%;
  background: #fff;
  padding: 36px 20px;
  margin-top: 17px;
  > div {
    display: inline-block;
    vertical-align: middle;
  }
  .swiper-button {
    width: 20px;
    height: 20px;
    cursor: pointer;
    text-align: center;
    padding: 0;
    font-weight: 500;
  }
  .swiper_ {
    width: calc(100% - 110px);
    margin: 0 30px;
    .item-device {
      width: 240px;
      .device {
        display: flex;
        .dev-img {
          width: 70px;
          height: 70px;
          img {
            width: 100%;
            height: 100%;
          }
        }
      }
    }
  }
}
</style>

实现效果:

在这里插入图片描述

### Vue使用 Swiper 实现滚动效果 #### 安装依赖 在 Vue 项目中集成 Swiper 插件前,需先安装 `swiper` 和其样式文件。通过 npm 或 yarn 进行安装: ```bash npm install swiper vue-awesome-swiper --save ``` 或者 ```bash yarn add swiper vue-awesome-swiper ``` 完成安装后,在项目的入口文件(如 main.js)引入 Swiper 的 CSS 文件。 ```javascript import 'swiper/swiper-bundle.css'; ``` --- #### 配置组件 创建一个自定义组件来封装 Swiper 功能。以下是基于 Vue 2.x 版本的示例代码[^2]。 ```vue <template> <div class="swiper-container"> <swiper :options="swiperOptions" ref="mySwiper"> <!-- 轮播项 --> <swiper-slide>Slide 1</swiper-slide> <swiper-slide>Slide 2</swiper-slide> <swiper-slide>Slide 3</swiper-slide> <!-- 分页器 --> <div class="swiper-pagination" slot="pagination"></div> <!-- 滚动条 --> <div class="swiper-scrollbar" slot="scrollbar"></div> </swiper> </div> </template> <script> import { swiper, swiperSlide } from "vue-awesome-swiper"; export default { name: "MySwiper", components: { swiper, swiperSlide, }, data() { return { swiperOptions: { slidesPerView: 1, spaceBetween: 30, loop: true, // 分页器配置 pagination: { el: ".swiper-pagination", clickable: true, // 启用点击分页功能[^1] }, // 滚动条配置 scrollbar: { el: ".swiper-scrollbar", draggable: true, // 启用拖拽滚动条功能 }, }, }; }, }; </script> <style scoped> .swiper-container { width: 100%; height: 100%; } </style> ``` 上述代码实现了基本的轮播图功能,并启用了分页器和滚动条的功能。 --- #### 样式调整 如果需要进一步优化样式,可以在 `<style>` 块中添加自定义样式。例如修改分页器的颜色或大小: ```css /* 修改分页器颜色 */ .swiper-pagination-bullet-active { background-color: red; } /* 修改滚动条高度 */ .swiper-scrollbar { height: 5px; bottom: 10px; /* 控制距离底部的距离 */ } ``` --- #### 切换到指定索引 为了实现手动切换到特定索引的功能,可以利用 `$refs.mySwiper.$swiper.slideTo(index)` 方法[^3]。以下是一个按钮触发切换的例子: ```vue <template> <div> <button @click="goToSlide(1)">跳转到第2张</button> <swiper :options="swiperOptions" ref="mySwiper"> <swiper-slide>Slide 1</swiper-slide> <swiper-slide>Slide 2</swiper-slide> <swiper-slide>Slide 3</swiper-slide> </swiper> </div> </template> <script> export default { methods: { goToSlide(index) { this.$refs.mySwiper.$swiper.slideTo(index); }, }, }; </script> ``` 此方法允许开发者灵活控制当前显示的内容。 ---
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值