一,安装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. Java中的数据类型转换

    先来看一个题: Java类Demo中存在方法func0.func1.func2.func3和func4,请问该方法中,哪些是不合法的定义?( ) public class Demo{ float fu ...

  2. Raspberry Pi - Huawei HiLink E3256 3G modem to ethernet adapter

    Raspberry Pi - Huawei HiLink E3256 3G modem to ethernet adapter This page documents how to configure ...

  3. Maximum Depth of Binary Tree 二叉树的深度

    Given a binary tree,find its maximum depth. The maximum depth is the number of nodes along the longe ...

  4. 《你不知道的JavaScript-上卷》笔记

    这段时间看了<你不知道的JavaScript>上卷,对很多知识有了重新的认识,所以在这里罗列一些知识点作为巩固. 作用域和闭包 词法作用域 变量赋值操作会执行的两个动作 答:编译器会在当前 ...

  5. Linux 系统安装[Redhat]2

    1.1. 配置网络 开机启动网卡eth0 1. 修改网络信息[root@Webserver ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 DEVI ...

  6. 乘风破浪:LeetCode真题_015_3Sum

    乘风破浪:LeetCode真题_015_3Sum 一.前言 关于集合的操作,也是编程最容易考试的问题,比如求集和中的3个元素使得它们的和为0,并且要求不重复出现,这样的问题该怎么样解决呢? 二.3Su ...

  7. SharePoint2007深入浅出——使用jQuery UI

    jQuery1.6.4.js + jQuery UI 1.8.16.js 只有这个版本在IE8下的兼容性视图+Quirks(文本模式),dialog可以正常显示.

  8. 安装visual studio2017后 首次启动出现ActivityLog.xml异常解决方法

    安装visual studio2017后 首次启动出现ActivityLog.xml异常解决方法 ps:操作系统是win10 在官网下载了vs2017社区版按照教程(教程链接在文末)安装完成之后,首次 ...

  9. 彻底关闭Firefox自动更新的方法

    自己最近在使用firefox进行自动化测试时,发现配置好的firefox总是会自动更新,主要是因为一些高版本的浏览器无法安装firepath,没有firepath的火狐浏览器使用起来总是有很多的不方便 ...

  10. 6、Node.js 事件循环

    #########################################################################################Node.js 事件循 ...