经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » JavaScript » 查看文章
vue中使用swiper并自定义分页器样式
来源:cnblogs  作者:Nicederen  时间:2019/3/29 15:09:46  对本文有异议

一,安装swiper

执行命令 npm install --save swiper

二,引入swiper

import Swiper from "swiper";
import "swiper/dist/css/swiper.css";
 
三,使用swiper,不废话,上代码。
<template>
  <div class="page">
    <div class="swiper-container">
      <div class="swiper-wrapper">
        <div class="swiper-slide">
          <img class="imgCard" src="../assets/swiper1.jpg" alt>
        </div>
        <div class="swiper-slide">
          <img class="imgCard" src="../assets/swiper2.jpg" alt>
        </div>
        <div class="swiper-slide">
          <img class="imgCard" src="../assets/swiper3.jpg" alt>
        </div>
      </div>
      <div class="swiper-pagination"></div>
    </div>
  </div>
</template>
<script>
import Swiper from "swiper";
import "swiper/dist/css/swiper.css";
export default {
  data() {
    return {
      dialogShow: false
    };
  },
  mounted() {
    this._initSwiper();
  },
  methods: {
    _initSwiper() {
      var mySwiper = new Swiper(".swiper-container", {
        direction: "horizontal",
        loop: true,
        autoplay: true, //自动轮播
        speed: 1000,
        pagination: {
          el: ".swiper-pagination",
          type: "custom",
          renderCustom: function(swiper, current, total) {
            var customPaginationHtml = "";
            for (var i = 0; i < total; i++) {
              //判断哪个分页器此刻应该被激活
              if (i == current - 1) {
                customPaginationHtml +=
                  '<span class="swiper-pagination-customs swiper-pagination-customs-active"></span>';
              } else {
                customPaginationHtml +=
                  '<span class="swiper-pagination-customs"></span>';
              }
            }
            return '<span class="swiperPag">'+customPaginationHtml+'</span>';
          }
        }
      });
    }
  }
};
</script>
<style lang="scss" >
.swiperPag {
  width:4.5rem;
  height: 0.07rem;
  border-radius: 0.04rem;
  display: flex;
  align-items: center;
  margin:0 auto;
  background-color: rgba($color: #000000, $alpha: 0.8)
}
.swiper-pagination-fraction, .swiper-pagination-custom, .swiper-container-horizontal > .swiper-pagination-bullets {
  bottom:0.27rem;
}
.swiper-pagination-customs {
  width: 1.5rem;
  height: 0.14rem;
  display: inline-block;
}
/*自定义分页器激活时的样式表现*/
.swiper-pagination-customs-active {
  width: 1.5rem;
  height: 0.14rem;
  display: inline-block;
  border-radius: 0.07rem;
  background-color: #28a7e1;
}
</style>

注意: style标签不要加scoped,否则样式加不上!

原文链接:http://www.cnblogs.com/nicederen/p/10620534.html

 友情链接: NPS