vue中使用swiper5做一个轮播图,带有分页器、左右箭头样式

折腾了半天,可算弄好了。记成文章,防止遗忘。

1.npm安装。由于swiper5以上才支持pagination分页器换颜色,所以我们安装swiper5以及vue-awesome-swiper

npm install swiper@5.4.4 vue-awesome-swiper --save

2.所有代码

<template>
  <div>
    <div>
      <swiper :options="swiperOptions" class="my-swiper">
        <swiper-slide> <img src="../assets/img/bg_1.jpg" /> </swiper-slide>
        <swiper-slide> <img src="../assets/img/bg_2.jpg" /> </swiper-slide>
        <swiper-slide> <img src="../assets/img/bg_3.jpg" /> </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>
  </div>
</template>

<script>

import {Swiper, SwiperSlide} from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'

export default {
  name: 'UserCenter',
  components: {
    Swiper,
    SwiperSlide
  },
  data () {
    return {
      swiperOptions: {
        // 循环
        loop: true,
        // 指示点
        pagination: {
          el: '.swiper-pagination',
          clickable: true /* 分页器点可以点击切换 */
        },
        // 方向:横向或者纵向vertical
        direction: 'horizontal',
        // 自动播放
        autoplay: {
          delay: 5000,
          disableOnInteraction: false
        },
        // 切换速度
        speed: 600,
        // 左右箭头按钮
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev'
        }
      }
    }
  },
  computed: {},
  mounted () {},
  methods: {}
}
</script>

<style scoped>
.my-swiper{
  width: 100%;
  height: auto;
  --swiper-navigation-color: white; /*左右箭头按钮颜色*/
  --swiper-pagination-color: white; /*pagination分页器颜色*/
}
.my-swiper img {
  width: 100%;
  height: auto;
}
</style>

最后,看成品。
在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

大能猫与小完熊

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

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

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

打赏作者

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

抵扣说明:

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

余额充值