Vue封装Swiper实现图片轮播效果


Posted in Javascript onFebruary 06, 2018

图片轮播是前端中经常需要实现的一个功能。最近学习Vue.js,就针对Swiper进行封装,实现一个简单的图片轮播组件。

一、Swiper

在实现封装之前,先介绍一下Swiper。

  • Swiper是纯Javascript打造的滑动特效插件,面向手机、平板电脑等移动终端。
  • Swiper能实现触屏焦点图、触屏Tab切换、触屏多图切换等常用效果。
  • Swiper开源、免费、稳定、使用简单、功能强大,是架构移动终端网站的重要选择。

Swiper的应用场景广泛,实现效果很好,下面个这实际案例就是Swiper的典型应用场景。

Vue封装Swiper实现图片轮播效果 

Swiper的具体使用教程及详细API,参考 Swiper中文网

二、Vue组件

Vue组件设计初衷就是要配合使用的,提高维护性和复用性。而图片轮播正适合使用组件来完成,因此在介绍具体的实现之前,先介绍下关于Vue组件及组件通信。

Vue组件中最常见的就是形成父子组件的关系:组件 A 在它的模板中使用了组件 B。

它们之间必然需要相互通信:父组件可能要给子组件下发数据,子组件则可能要将它内部发生的事情告知父组件。然而,通过一个良好定义的接口来尽可能将父子组件解耦也是很重要的。这保证了每个组件的代码可以在相对隔离的环境中书写和理解,从而提高了其可维护性和复用性。

在 Vue 中,父子组件的关系可以总结为 prop 向下传递,事件向上传递。父组件通过 prop 给子组件下发数据,子组件通过事件给父组件发送消息。

Vue封装Swiper实现图片轮播效果 

三、封装实现

1.引入Swiper

首先,需要安装Swiper。

npm install --save swiper

然后,要引用两个文件。

import Swiper from "swiper";
import "swiper/dist/css/swiper.min.css";

2.HTML代码

在模板中设置轮播图的html布局。

<template>
 <div class="swiper-container" :class="swipeid">
  <div class="swiper-wrapper">
   <!-- 存放具体的轮播内容 -->
   <slot name ="swiper-con"></slot>
  </div>
  <!-- 分页器 -->
  <div :class="{'swiper-pagination':pagination}"></div>
 </div>
</template>

其中使用具名插槽,提高解耦,使得在父组件使用时,根据不同情况,设置不同的轮播内容。

另外需要设置分页器,即图片轮播中的页面指示器,常见的如小圆点,或者数字指示器。

3.初始化Swiper

既然是对Swiper进行封装实现轮播图,前面也已经安装了Swiper,那么现在就需要初始化使用。

在初始化之前,根据Swiper用法的了解,先确定轮播组件需要的属性信息,然后通过父组件传递给封装的Swiper组件。

这时候就需要用到props。

props: {
 swipeid: {
  type: String,
  default: ""
 },
 effect: {
  type: String,
  default: "slide"
 },
 loop: {
  type: Boolean,
  default: false
 },
 direction: {
  type: String,
  default: "horizontal"
 },
 pagination: {
  type: Boolean,
  default: true
 },
 paginationType: {
  type: String,
  default: "bullets"
 },
 autoPlay: {
  type: Number,
  default: 3000
 }
 }

下面逐一解释每个属性的含义。

属性 含义
swiped 轮播容器class属性的类名。
effect 图片的 切换效果,默认为"slide",还可设置为"fade", "cube", "coverflow","flip",详情见effect。
loop 设置为true 则开启loop模式。loop模式:会在原本图片前后复制若干个图片并在合适的时候切换,让Swiper看起来是循环的,详情见loop。
direction 图片的滑动方向,可设置水平(horizontal)或垂直(vertical),详情见direction。
pagination 使用分页导航,详情见pagination。
paginationType 分页器样式类型,可设置为"bullets", "fraction", "progressbar", "custom",详情见type。
autoPlay 设置为true启动自动切换,并使用默认的切换设置,详情见autoplay。

了解了上面每个属性的含义,下面就可以初始化Swiper,并设置具体的属性。

初始化Swiper时,需要传入两个参数。

  • 轮播容器的类名
  • 代表图片轮播组件详细功能的对象
var that = this;
 this.dom = new Swiper("." + that.swipeid, {
  //循环
  loop: that.loop,
  //分页器
  pagination: { 
  el: ".swiper-pagination",
  bulletClass : 'swiper-pagination-bullet',
   },
  //分页类型
  paginationType: that.paginationType,
  //自动播放
  autoPlay: that.autoPlay,
  //方向
  direction: that.direction,
  //特效
  effect: that.effect,
  //用户操作swiper之后,不禁止autoplay
  disableOnInteraction: false,
  //修改swiper自己或子元素时,自动初始化swiper
  observer: true,
  //修改swiper的父元素时,自动初始化swiper
  observeParents: true
 });
 }

四、自定义轮播效果

经过上面的步骤,轮播器就封装好了。我们可以自定义实现自己想要的轮播器效果。下面以知乎的API为例,实现图片轮播。

1.HTML代码

<m-swipe swipeid="swipe" ref="swiper" :autoPlay="3000" effect="slide">
  <div v-for="top in tops" :key="top.id" class="swiper-slide" slot="swiper-con" >
  <img :src="top.image">
  <h3>{{top.title}}</h3>
  </div>
</m-swipe>

首先要引用注册组件,这里就不详细写出。

其中 m-swipe 就是前面实现的图片轮播组件,而其中的子组件就是通过具名插槽插入的轮播内容。

2.CSS代码

.swiper-container {
 width: 100%;
 }
 .swiper-slide {
 height: 8rem;
 overflow: hidden;
 position: relative;
 }
.swiper-slide {
 div {
 top: 0;
 left: 0;
 width: 100%;
 height: 100%;
 opacity: 0.4;
 position: absolute;
 background-color: @blue;
 }
 img {
 top: 50%;
 position: relative;
 transform: translate(0, -50%);
 }
 h3 {
 width: 70%;
 color: #fff;
 margin: 0;
 font-size: 0.5rem;
 line-height: 1rem;
 right: 5%;
 bottom: 2.6rem;
 text-align: right;
 position: absolute;
 text-shadow: 1px 1px 10px rgba(0, 0, 0, 0.5);
 &:before {
  content: "";
  width: 3rem;
  bottom: -0.6rem;
  right: 0;
  display: block;
  position: absolute;
  border: 2px solid @yellow;
 }
 }
}
.swiper-pagination-bullet-active {
 background: #fff;
}
.swiper-container-horizontal > .swiper-pagination-bullets {
 bottom: 1rem;
 width: 95%;
 text-align: right;
 }

其中 swiper-pagination-bullet-active 代表分页器中当前指示的小圆点的类名。 .swiper-pagination-bullets 代表分页器的类名,详情见pagination分页器内元素的类名 。

关于网络请求数据展示的代码就不贴了,下面有源码地址。

3.效果

Vue封装Swiper实现图片轮播效果 

这只是一个简单的封装效果,想要实现更多的效果,可以通过Swiper中提供的更多功能来实现。

Github地址: 图片轮播

总结

以上所述是小编给大家介绍的Vue封装Swiper实现图片轮播效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Javascript 相关文章推荐
js文件中调用js的实现方法小结
Oct 23 Javascript
jquery 动态创建元素的方式介绍及应用
Apr 21 Javascript
关于jQuery中的each方法(jQuery到底干了什么)
Mar 05 Javascript
jquery ajax应用中iframe自适应高度问题解决方法
Apr 12 Javascript
javascript处理表单示例(javascript提交表单)
Apr 28 Javascript
node.js中的url.parse方法使用说明
Dec 10 Javascript
Node.js获取前端ajax提交的request信息
Feb 20 Javascript
从零学习node.js之简易的网络爬虫(四)
Feb 22 Javascript
Vue实现微信支付功能遇到的坑
Jun 05 Javascript
Node.js API详解之 net模块实例分析
May 18 Javascript
vue实现公共方法抽离
Jul 31 Javascript
JS面向对象实现飞机大战
Aug 26 Javascript
vue中如何使用ztree
Feb 06 #Javascript
javascript回调函数详解
Feb 06 #Javascript
原生JS实现瀑布流插件
Feb 06 #Javascript
JS实现的将html转为pdf功能【基于浏览器端插件jsPDF】
Feb 06 #Javascript
20行JS代码实现粘贴板复制功能
Feb 06 #Javascript
JS中offset和匀速动画详解
Feb 06 #Javascript
Bootstrap实现的表格合并单元格示例
Feb 06 #Javascript
You might like
PHP4之真OO
2006/10/09 PHP
Apache 配置详解(最好的APACHE配置教程)
2010/07/04 PHP
PHP的autoload自动加载机制使用说明
2010/12/28 PHP
Smarty变量调节器失效的解决办法
2014/08/20 PHP
PHP中strpos、strstr和stripos、stristr函数分析
2016/06/11 PHP
thinkPHP数据查询常用方法总结【select,find,getField,query】
2017/03/15 PHP
PHP XML Expat解析器知识点总结
2019/02/15 PHP
PHP+jQuery实现即点即改功能示例
2019/02/21 PHP
php提供实现反射的方法和实例代码
2019/09/17 PHP
jquery下操作HTML控件的实现代码
2010/01/12 Javascript
JS中confirm,alert,prompt函数区别分析
2011/01/17 Javascript
jquery select动态加载选择(兼容各种浏览器)
2013/02/01 Javascript
基于jQuery选择器的整理集合
2013/04/26 Javascript
jQuery中复合属性选择器用法实例
2014/12/31 Javascript
简介JavaScript中Math.cos()余弦方法的使用
2015/06/15 Javascript
jQuery简单实现验证邮箱格式
2015/07/15 Javascript
浅谈bootstrap源码分析之scrollspy(滚动侦听)
2016/06/06 Javascript
Vue 中对图片地址进行拼接的方法
2018/09/03 Javascript
微信小程序实现购物车功能
2020/11/18 Javascript
Python基础语法(Python基础知识点)
2016/02/28 Python
python实现图像拼接
2020/03/05 Python
浅谈OpenCV中的新函数connectedComponentsWithStats用法
2020/07/05 Python
Python classmethod装饰器原理及用法解析
2020/10/17 Python
python 基于pygame实现俄罗斯方块
2021/03/02 Python
阿迪达斯加拿大官网:Adidas加拿大
2016/08/25 全球购物
购买英国原创艺术:Art Gallery
2018/08/25 全球购物
如何保障Web服务器安全
2014/05/05 面试题
C#面试题
2016/05/06 面试题
技校生自我鉴定
2013/12/08 职场文书
考试不及格检讨书
2014/01/09 职场文书
远程研修随笔感言
2014/02/10 职场文书
年度安全生产目标责任书
2014/07/23 职场文书
纪念九一八事变演讲稿:勿忘国耻
2014/09/14 职场文书
图解上海144收音机
2021/04/22 无线电
Go语言中的UTF-8实现
2021/04/26 Golang
tp5使用layui实现多个图片上传(带附件选择)的方法实例
2021/11/17 PHP