详解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 相关文章推荐
extjs实现选择多表自定义查询功能 前台部分(ext源码)
Dec 20 Javascript
jQuery中:reset选择器用法实例
Jan 04 Javascript
浅谈JavaScript中的String对象常用方法
Feb 25 Javascript
js闭包引起的事件注册问题介绍
Mar 29 Javascript
Bootstrap响应式侧边栏改进版
Sep 17 Javascript
BootStrap下拉菜单和滚动监听插件实现代码
Sep 26 Javascript
AngularJS模仿Form表单提交的实现代码
Dec 08 Javascript
利用jquery实现实时更新歌词的方法
Jan 06 Javascript
Angular模版驱动表单的使用总结
May 05 Javascript
vue.js通过路由实现经典的三栏布局实例代码
Jul 08 Javascript
在layui下对元素进行事件绑定的实例
Sep 06 Javascript
JavaScript内置对象之Array的使用小结
May 12 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
PHP安全性漫谈
2012/06/28 PHP
PHP使用正则表达式清除超链接文本
2013/11/12 PHP
javascript some()函数用法详解
2014/11/13 PHP
php中简单的对称加密算法实现
2017/01/05 PHP
JQuery live函数
2010/12/24 Javascript
javascript面向对象特性代码实例
2014/06/12 Javascript
JavaScript中实现继承的三种方式和实例
2015/01/29 Javascript
jQuery简单注册和禁用全局事件的方法
2016/07/25 Javascript
详解Angualr 组件间通信
2017/01/21 Javascript
Ionic+AngularJS实现登录和注册带验证功能
2017/02/09 Javascript
详解刷新页面vuex数据不消失和不跳转页面的解决
2018/01/30 Javascript
微信小程序导航栏跟随滑动效果的实现代码
2019/05/14 Javascript
javascript实现商品图片放大镜
2019/11/28 Javascript
js+css实现全屏侧边栏
2020/06/16 Javascript
vue-cli3中配置alias和打包加hash值操作
2020/09/04 Javascript
js 将多个对象合并成一个对象 assign方法的实现
2020/09/24 Javascript
微信跳一跳小游戏python脚本
2018/01/05 Python
python中的句柄操作的方法示例
2019/06/20 Python
python读取指定字节长度的文本方法
2019/08/27 Python
简述python&amp;pytorch 随机种子的实现
2020/10/07 Python
鲜为人知的HTML5语音合成功能
2019/05/17 HTML / CSS
Canvas获取视频第一帧缩略图的实现
2020/11/11 HTML / CSS
意大利网上书店:LaFeltrinelli
2020/06/12 全球购物
如何使用PHP session
2015/04/21 面试题
《我的第一本书》教学反思
2014/02/15 职场文书
中班中秋节活动反思
2014/02/18 职场文书
物理系毕业生自荐书范文
2014/02/22 职场文书
医学生职业生涯规划书范文
2014/03/13 职场文书
客户付款通知书
2015/04/23 职场文书
浅谈:电影《孔子》观后感(范文)
2019/10/14 职场文书
SpringAop日志找不到方法的处理
2021/06/21 Java/Android
解决mysql:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO/YES)
2021/06/26 MySQL
使用@Value值注入及配置文件组件扫描
2021/07/09 Java/Android
Vue Element-ui表单校验规则实现
2021/07/09 Vue.js
5种 JavaScript 方式实现数组扁平化
2021/10/05 Javascript
Java的Object类的九种方法
2022/04/13 Java/Android