Vue中引入swiper 自动轮播

安装swiper

cnpm i swiper vue-awesome-swiper --save

在main.js中引入

import VueAwesomeSwiper from 'vue-awesome-swiper'
import 'swiper/dist/css/swiper.min.css'

Vue.use(VueAwesomeSwiper)

在相关组件使用

<div class="system-swiper">
  <swiper :options="swiperOption" ref="mySwiper" v-if="imgNewsList.length">
    <!-- v-if 用来判断是否为空 不判断的话不回循环 -->
    <swiper-slide v-for="(item, index) in imgNewsList" :key="index">
      <router-link :to="{ path: '***', query: { id: item.Id } }">
        <img style="height: 150px; width: 190px" :src="item.imgPath" />
        <p>{{ item.xwBt }}</p>
      </router-link>
    </swiper-slide>
  </swiper>
  <div class="swiper-button-prev" slot="button-prev"></div>
  <div class="swiper-button-next" slot="button-next"></div>
</div>

初始化 data数据

//swiper 初始化
swiperOption: {
  slidesPerView: 5, // 显示数量
  spaceBetween: 30,  // 之间的距离(px)
  loop: true, // 是否开启循环
  navigation: {
    nextEl: ".swiper-button-next", // 绑定上一个按钮
    prevEl: ".swiper-button-prev", // 绑定下一个按钮
  },
  autoplay: {
    delay: 2500,  // 多长时间进行一次自动切换
    disableOnInteraction: false,  // 用户点击一次后是否取消自动切换 默认true
  },
  speed: 300, // 切换速度
},

 相关样式

.system-swiper {
  width: 1200px;
  overflow: hidden;
  margin: 0px auto 0;
  height: 182px;
  background-color: #ffffff;
  text-align: center;
  position: relative;
  .swiper-slide {
    width: 216px;
    height: 173px;
  }
  margin-top: 10px;
  p {
    font-size: 14px;
  }
  img {
    display: inline-block;
    padding: 4px;
    border: 1px solid #bebebe;
  }
}
.swiper-button-next {
  background: url(../assets/img/btn-you.png) no-repeat;
  background-position: 7px 8px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  right: -18px;
  background-color: rgba(0, 0, 0, 0.35);
}
.swiper-button-prev {
  background: url(../assets/img/btn-zuo.png) no-repeat;
  background-position: 19px 8px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  left: -18px;
  background-color: rgba(0, 0, 0, 0.35);
}

  • 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、付费专栏及课程。

余额充值