详解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各种复制代码收集
Sep 20 Javascript
jquery 页面全选框实践代码
Apr 02 Javascript
理解Javascript_08_函数对象
Oct 15 Javascript
Bootstrap Paginator分页插件与ajax相结合实现动态无刷新分页效果
May 27 Javascript
jQuery事件对象总结
Oct 17 Javascript
bootstrap输入框组件使用方法详解
Jan 19 Javascript
js实现简易聊天对话框
Aug 17 Javascript
AngularJS实现的2048小游戏功能【附源码下载】
Jan 03 Javascript
Servlet3.0与纯javascript通过Ajax交互的实例详解
Mar 18 Javascript
vue实现弹幕功能
Oct 25 Javascript
JS如何实现动态添加的元素绑定事件
Nov 12 Javascript
Vue-cli3多页面配置详解
Mar 22 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
一个从别的网站抓取信息的例子(域名查询)
2006/10/09 PHP
PHP根据传来的16进制颜色代码自动改变背景颜色
2014/06/13 PHP
PHP/HTML混写的四种方式总结
2017/02/27 PHP
php 调用ffmpeg获取视频信息的简单实现
2017/04/03 PHP
php中array_fill函数的实例用法
2021/03/02 PHP
document.all还是document.getElementsByName?
2006/07/21 Javascript
IE6下focus与blur错乱的解决方案
2011/07/31 Javascript
jquery聚焦文本框与扩展文本框聚焦方法
2012/10/12 Javascript
转义字符(\)对JavaScript中JSON.parse的影响概述
2013/07/17 Javascript
JS创建类和对象的两种不同方式
2014/08/08 Javascript
微信小程序 wxapp视图容器 view详解
2016/10/31 Javascript
使用Bootstrap + Vue.js实现添加删除数据示例
2017/02/27 Javascript
JavaScript时间戳与时间日期间相互转换
2017/12/11 Javascript
vue如何截取字符串
2019/05/06 Javascript
layui table设置某一行的字体颜色方法
2019/09/05 Javascript
JS将指定的某个字符全部转换为其他字符实例代码
2020/10/13 Javascript
[01:01:51]EG vs VG Supermajor小组赛B组 BO3 第二场 6.2
2018/06/03 DOTA
[38:23]完美世界DOTA2联赛循环赛 FTD vs PXG BO2第二场 11.01
2020/11/02 DOTA
python的re模块应用实例
2014/09/26 Python
python自然语言编码转换模块codecs介绍
2015/04/08 Python
Python基于回溯法子集树模板解决0-1背包问题实例
2017/09/02 Python
详解python3中zipfile模块用法
2018/06/18 Python
python中plot实现即时数据动态显示方法
2018/06/22 Python
python 实现求解字符串集的最长公共前缀方法
2018/07/20 Python
ubuntu16.04制作vim和python3的开发环境
2018/09/23 Python
如何在python中写hive脚本
2019/11/08 Python
使用OpenCV获取图片连通域数量,并用不同颜色标记函
2020/06/04 Python
Tensorflow全局设置可见GPU编号操作
2020/06/30 Python
Python 实现简单的客户端认证
2020/07/29 Python
关于Python不换行输出和不换行输出end=““不显示的问题(亲测已解决)
2020/10/27 Python
艺术专业大学生自我评价
2013/09/22 职场文书
计算机数据库专业职业生涯规划书
2014/02/08 职场文书
运动会广播稿诗歌版
2014/09/12 职场文书
个人投资合作协议书
2014/10/12 职场文书
大学生创业事迹材料
2014/12/30 职场文书
2015年大学组织委员个人工作总结
2015/10/23 职场文书