详解swiper在vue中的应用(以3.0为例)


Posted in Javascript onSeptember 20, 2018

一、使用方法

官网地址

参考此文章(点击我)

二、常见情况

图片需要动态获取时

待数据加载成功且渲染完毕后,进行节点初始化。例:

axios.post(‘接口地址', 参数).then(response => {  
  this.pages = response.data; //pages 渲染数据的数组
  this.$nextTick(() => { //渲染结束
    // mySwiper 里面的属性按需配置,详情见官网
    let mySwiper = new Swiper(".swiper-container", { 
      initialSlide :0,//默认播放(值为图片下标)
      loop: false,//不循环
      speed: 600, //切换速度
      paginationClickable: true, //点击小点可以切换
    });
   });
});

当有 3 组图片需要依次播放时(多组以此类推)

情景:第 2 组图片滑动最后一张时,需要加载第 3 组图片;第 2 组图片滑动第一张时,需要加载第 1 组图片。

方法:在初始化的时候,配置回调函数onTouchStart(开始滑动的X轴值)和 onTouchEnd(结束滑动的X轴值)。用差值来判断是往前滑还是往后划。

this.$nextTick(() => {
  let mySwiper = new Swiper(".swiper-container", {
   observer: true, //修改swiper自己或子元素时,自动初始化swiper
   observeParents: true, //修改swiper的父元素时,自动初始化swiper     
   onTouchStart: function(swiper) {
    this.positionStart = swiper.getWrapperTranslate();
   },
   onTouchEnd: function(swiper) {
    this.positionEnd = swiper.getWrapperTranslate();
    if (this.positionStart > this.positionEnd && this.pages.length - 1 == this.mySwiper.activeIndex) { 
      //向后滑,加载后一组图片       
    } else if (mySwiper.activeIndex == 0 && this.positionStart < this.positionEnd) {
      //向前划,加载前一组图片  
    }
   }
  });
 });

这时,图片已经加载了另外一组图片,但是各组图片之间没有连贯起来,这就需要用到 slideTo方法去跳转(若加载第 3 组,则跳转到下标第 0 个;若加载第 1 组,则跳转到下标第 图片个数-1 个)。

mySwiper.slideTo('要跳转的图片下标', 10, false) // 10表示跳转速度;false 代表是否触发回到函数

数据方法配置

export default {
 name: '',
 data() {
  return {
   swiperOption: {
    // NotNextTick is a component's own property, and if notNextTick is set to true, the component will not instantiate the swiper through NextTick, which means you can get the swiper object the first time (if you need to use the get swiper object to do what Things, then this property must be true)
    // notNextTick是一个组件自有属性,如果notNextTick设置为true,组件则不会通过NextTick来实例化swiper,也就意味着你可以在第一时间获取到swiper对象,假如你需要刚加载遍使用获取swiper对象来做什么事,那么这个属性一定要是true
    notNextTick: true,
    // swiper configs 所有的配置同swiper官方api配置
    autoplay: 3000,
    // direction : 'vertical',
    effect:"coverflow",
    grabCursor : true,
    setWrapperSize :true,
    // autoHeight: true,
    // paginationType:"bullets",
    pagination : '.swiper-pagination',
    paginationClickable :true,
    prevButton:'.swiper-button-prev',
    nextButton:'.swiper-button-next',
    // scrollbar:'.swiper-scrollbar',
    mousewheelControl : true,
    observeParents:true,
    // if you need use plugins in the swiper, you can config in here like this
    // 如果自行设计了插件,那么插件的一些配置相关参数,也应该出现在这个对象中,如下debugger
    // debugger: true,
    // swiper callbacks
    // swiper的各种回调函数也可以出现在这个对象中,和swiper官方一样
    // onTransitionStart(swiper){
    //  console.log(swiper)
    // },
    // more Swiper configs and callbacks...
    // ...
   }
  }
 },components: {
 swiper,
 swiperSlide
},
 // you can find current swiper instance object like this, while the notNextTick property value must be true
 // 如果你需要得到当前的swiper对象来做一些事情,你可以像下面这样定义一个方法属性来获取当前的swiper对象,同时notNextTick必须为true
 computed: {
  swiper() {
   return this.$refs.mySwiper.swiper
  }
 },
 mounted() {
  // you can use current swiper instance object to do something(swiper methods)
  // 然后你就可以使用当前上下文内的swiper对象去做你想做的事了
  // console.log('this is current swiper instance object', this.swiper)
  // this.swiper.slideTo(3, 1000, false)
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
javascript hashtable 修正版 下载
Dec 30 Javascript
Jquery判断$(&quot;#id&quot;)获取的对象是否存在的方法
Sep 25 Javascript
Js实现手机发送验证码时按钮延迟操作
Jun 20 Javascript
jQuery实现MSN中文网滑动Tab菜单效果代码
Sep 09 Javascript
详解照片瀑布流效果(js,jquery分别实现与知识点总结)
Jan 01 Javascript
jquery 实时监听输入框值变化的完美方法(必看)
Jan 26 Javascript
浅谈angular2的http请求返回结果的subcribe注意事项
Mar 01 Javascript
jQuery实现可兼容IE6的遮罩功能详解
Sep 19 jQuery
JavaScript设计模式之观察者模式(发布订阅模式)原理与实现方法示例
Jul 27 Javascript
p5.js临摹旋转爱心
Oct 23 Javascript
JS端基于download.js实现图片、视频时直接下载而不是打开预览
May 09 Javascript
Vue如何基于vue-i18n实现多国语言兼容
Jul 17 Javascript
Vue框架里使用Swiper的方法示例
Sep 20 #Javascript
vue项目中跳转到外部链接的实例讲解
Sep 20 #Javascript
Vue常见面试题整理【值得收藏】
Sep 20 #Javascript
用vue-cli开发vue时的代理设置方法
Sep 20 #Javascript
CSS3 动画卡顿性能优化的完美解决方案
Sep 20 #Javascript
vue.js中proxyTable 转发请求的实现方法
Sep 20 #Javascript
浅谈React Event实现原理
Sep 20 #Javascript
You might like
smtp邮件发送一例
2006/10/09 PHP
php trim 去除空字符的定义与语法介绍
2010/05/31 PHP
Linux编译升级php的详细方法
2013/11/04 PHP
PHP中文竖排转换实现方法
2015/10/23 PHP
PHP中对数组的一些常用的增、删、插操作函数总结
2015/11/27 PHP
event.srcElement 用法笔记e.target
2009/12/18 Javascript
juqery 学习之四 筛选查找
2010/11/30 Javascript
JS声明变量背后的编译原理剖析
2012/12/28 Javascript
详谈javascript中DOM的基本属性
2015/02/26 Javascript
jQuery+json实现的简易Ajax调用实例
2015/12/14 Javascript
JavaScript函数绑定用法实例分析
2017/11/14 Javascript
JavaScript中的一些隐式转换和总结(推荐)
2017/12/22 Javascript
微信小程序数字滚动插件使用详解
2018/02/02 Javascript
Vue头像处理方案小结
2018/07/26 Javascript
如何在vue里添加好看的lottie动画
2018/08/02 Javascript
vue中监听路由参数的变化及方法
2019/12/06 Javascript
JavaScript中继承原理与用法实例入门
2020/05/09 Javascript
[01:37]PWL S2开团时刻DAY1&2——这符有毒
2020/11/20 DOTA
Python中的defaultdict模块和namedtuple模块的简单入门指南
2015/04/01 Python
Python tkinter模块弹出窗口及传值回到主窗口操作详解
2017/07/28 Python
pycharm: 恢复(reset) 误删文件的方法
2018/10/22 Python
python中resample函数实现重采样和降采样代码
2020/02/25 Python
CSS3 实现图形下落动画效果
2020/11/13 HTML / CSS
全球酒店预订网站:Hotels.com
2016/08/10 全球购物
阳光体育:Sunny Sports(购买露营和远足设备)
2018/08/07 全球购物
SKECHERS斯凯奇中国官网:来自美国的运动休闲品牌
2018/11/14 全球购物
大学同学聚会邀请函
2014/01/19 职场文书
优秀士兵先进事迹
2014/02/06 职场文书
公司中层干部的自我评价分享
2014/03/01 职场文书
2014光棍节大学生联谊活动方案
2014/10/10 职场文书
初婚初育证明范本
2014/11/24 职场文书
2015年社区文体活动总结
2015/03/25 职场文书
小组口号霸气押韵
2015/12/24 职场文书
《迟到》教学反思
2016/02/24 职场文书
教你怎么用python爬取爱奇艺热门电影
2021/05/20 Python
关于python类SortedList详解
2021/09/04 Python