使用 vue-awesome-swiper 制作简单的轮播图

一. 安装 vue-awesome-swiper

npm install vue-awesome-swiper@3 --save-dev   // 安装指定版本

二. 封装轮播组件

1. HTML

<template>
  <div class="swipe">
    <div class="title">主讲人</div>
    <div class="recommendPage">
      <swiper :options="swiperOption" ref="mySwiper">
        <swiper-slide v-for="item in teachersArr" :key="item.teacherNo">
          <div class="box">
            <div class="box_img">
              <img :src="item.picture" />
            </div>
            <div class="box_text">
              <div>{{ item.fullName }}</div>
              <div>{{ item.title }}</div>
            </div>
          </div>
        </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>

2. JS

<script>
// 引入插件
import { swiper, swiperSlide } from "vue-awesome-swiper";
import "swiper/dist/css/swiper.css";
export default {
  props: ["teachersArr"],
  data() {
    return {
      swiperOption: {
        loop: true,
        autoplay: {
           delay: 3000,
           stopOnLastSlide: false,
           disableOnInteraction: false,
        },
        // 显示分页
        pagination: {
          el: ".swiper-pagination",
          clickable: true, //允许分页点击跳转
        },
        // 设置点击箭头
         navigation: {
           nextEl: ".swiper-button-next",
           prevEl: ".swiper-button-prev",
        },
      },
    };
  },
  components: { swiper, swiperSlide },
  computed: {
    swiper() {
      return this.$refs.mySwiper.swiper;
    },
  },
  mounted() {
    // current swiper instance
    // 然后你就可以使用当前上下文内的swiper对象去做你想做的事了
    // console.log("this is current swiper instance object", this.swiper);
    // this.swiper.slideTo(3, 1000, false);
  },
};
</script>

CSS

<style lang="less" scoped>
.recommendPage .swiper-container {
  position: relative;
  width: 100%;
  height: 0.8rem;
  background: #fff;
}
.recommendPage .swiper-container .swiper-slide {
  width: 100%;
  background: #fafafa;
  color: #000;
  font-size: 0.16rem;
}
.swipe {
  margin: 0.1rem 0;
  padding: 0 0.16rem 0.16rem;
  background-color: #fff;
  .title {
    margin-left: 0.2rem;
    line-height: 0.6rem;
    font-size: 0.18rem;
    font-weight: 600;
  }
}
.box {
  display: flex;
  padding: 0.15rem 0.1rem;
  .box_img {
    img {
      width: 0.5rem;
      height: 0.5rem;
      border-radius: 50%;
      border: 0.01rem solid #ffd200;
    }
  }
  .box_text {
    margin-left: 0.2rem;
    div {
      &:nth-child(1) {
        margin-top: 0.05rem;
        font-size: 0.15rem;
        font-weight: 600;
      }
      &:nth-child(2) {
        margin-top: 0.1rem;
        font-size: 0.1rem;
        color: #ccc;
      }
    }
  }
}
.swiper-pagination-fraction,
.swiper-pagination-custom,
.swiper-container-horizontal > .swiper-pagination-bullets {
  bottom: 0;
}
/deep/.swiper-pagination-bullet-active {
  background: #ffd200;
}
/deep/.swiper-pagination-bullet {
  width: 0.04rem;
  height: 0.04rem;
}
</style>

三. 在页面中引入轮播组件

1. HTML

<TeacherSwipe v-if="form.teachers && form.teachers.length" :teachersArr="form.teachers" />
<!-- 为什么要用 v-if, 因为数组是请求回来的, 要确保它有值才能, 不然就容易导致轮播图不能循环   -->

2. JS

<script>
import TeacherSwipe from "@/components/course/TeacherSwipe.vue";
export default {
	components: { TeacherSwipe },
}
</script>

四. 效果图

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值