一,安装swiper

执行命令 npm install vue-awesome-swiper --save

二,引入swiper

import {Swiper} from "vue-awesome-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,否则样式加不上!
  • 直接npm install swiper --save 下载的是swiper4,build打包时,会报错如下: Unexpected token: name (Dom7) [./~/swiper/~/dom7/dist/dom7.modular.js:16,0][static/js/vendor.cf492f2bb7f8b02ec428.js:16311,6]

到后来才发现,这样写是有问题的,当路由切换后再次进入该页面轮播就停止了,然后就做了如下更改。

export default {
data() {
return {
dialogShow: false,
mySwiper: {},
};
},
activated() {
this._initSwiper(); // 初始化swiper
},
deactivated() {
this.mySwiper.destroy();// 销毁swiper
},
methods: {
_initSwiper() {
this.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>';
}
}
});
}
}
};

vue中使用swiper并自定义分页器样式的更多相关文章

  1. vue中修改swiper样式

    问题 vue单文件组件中无法修改swiper样式. 解决 1,单文件组件中:新增一个style 不加scoped 让它最终成为全局样式.只在其中操作swiper的样式. <style lang= ...

  2. vue中引用swiper轮播插件

    有时候我们需要在vue中使用轮播组件,如果是在vue组件中引入第三方组件的话,最好通过npm安装,从而进行统一安装包管理. 申明:本文所使用的是vue.2x版本. 通过npm安装插件: npm ins ...

  3. 在vue中使用swiper组件

    第一步:在终端的项目根目录下载安装swiper: cnpm/npm install vue-awesome-swiper --save; 第二步:在程序入口文件main.js中引用: import V ...

  4. vue中引入swiper(vue中的滑块组件vue-awesome-swiper)

    第一步安装 npm install vue-awesome-swiper --save 第二部在main.js中引入 import VueAwesomeSwiper from 'vue-awesome ...

  5. 【Vue中的swiper轮播组件】

    <template> <swiper :options="swiperOption" ref="mySwiper"> <!-- s ...

  6. vue中使用Element主题自定义肤色

    一.搭建好项目的环境. 二.根据ElementUI官网的自定义主题(http://element.eleme.io/#/zh-CN/component/custom-theme)来安装[主题生成工具] ...

  7. vue中添加swiper轮播插件

    网上找了很多,最后还是官网最完整. https://github.com/surmon-china/vue-awesome-swiper 安装: 1.npm install vue-awesome-s ...

  8. vue中引入swiper插件

    这里我们使用npm的方式安装swiper插件. 1.npm install vue-awesome-swiper --save 2.在main.js文件中引入文件 import Vue from 'v ...

  9. vue中的swiper element ui

    欢迎加入前端交流群交流知识&&获取视频资料:749539640 很多同学问,怎么把swiper引入到vue的脚手架里去,之前的一篇博客有提到怎么引入,但是后来感觉不怎么好,还是用一些v ...

随机推荐

  1. Django—Form两种解决表单数据无法动态刷新的方法

    一.无法动态更新数据的实例 1. 如下,数据库中创建了班级表和教师表,两张表的对应关系为“多对多” from django.db import models class Classes(models. ...

  2. windows&cmd常用命令&快捷键

    1: cls即CLear Screen 功能:清除屏幕上的所有显示 2:Win+Tab快捷键进行3D窗口切换还有比较快速切换窗口的新方法. 3:利用Alt+Tab快捷键进行切换窗口时,在桌面中间会显示 ...

  3. Androidpdf

    https://www.jb51.net/article/110238.htm https://blog.csdn.net/u010046908/article/details/53927157 &l ...

  4. C#-MDI-多窗口通信方法总结

    总结C#中窗体间传递数据的几种方法 (由别人的方法整理) 转自:http://blog.csdn.net/daisy423xu/article/details/1579854 在编写C#windows ...

  5. 学习笔记:Nginx反射代理使用缓存和删除其缓存文件的方法

    使用nginx做cache服务器 需求就是缓存android的软件包,后缀名是apk.话不多说,直接上配置,供参考: a-->nginx.conf user www www; worker_pr ...

  6. poj 1475 推箱子

    bfs是一层层的遍历下去,每多一层即为多走一步,因此只要遇到T就停,此时肯定是最小步数. 所以这两层bfs应为,先对箱子的最少步数进行bfs,从而求出推箱子所用最少步数: 然后箱子bfs内部嵌入人的b ...

  7. 沉淀,再出发:web前端的一些认识

    沉淀,再出发:web前端的一些认识 一.前言 作为程序员,我一直认为全栈是一种最基本的能力,没有了这种目标就会发现自己越往后面发展路就越窄,很多自己不了解的东西会阻塞自己去理解整个系统的开发过程和效率 ...

  8. August 16th 2017 Week 33rd Wednesday

    A man can be destroyed but not defeated. 一个人可以被毁灭,但不能被打败. Before he was destroyed, he would have bee ...

  9. js布局库

    1.viz.js The solution was that someone cross compiled Graphviz to Javascript using llvm + emscripten ...

  10. css3鼠标经过出现转圈菜单(仿)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...