移动端轮播图插件,在使用iview图形界面插件中的carousel组件无法实现触摸滑动后,转而使用vue-awesome-swiper插件

1.npm安装


npm i vue-awesome-swiper -S
  • 我这里安装的是下面的这个版本

2.使用

  • 全局导入:

import Vue from 'vue'
import vueSwiper from 'vue-awesome-swiper'
/* 样式的话,我这里有用到分页器,就在全局中引入了样式 */
import 'swiper/dist/css/swiper.css'
Vue.use(vueSwiper);
  • 组件引入

import { swiper, swiperSlide } from "vue-awesome-swiper";
require("swiper/dist/css/swiper.css");
components: {
swiper,
swiperSlide
},
  • 在template中使用

<swiper :options="swiperOption" class="swiper-wrap" ref="mySwiper" v-if="banner.length!=0">
<swiper-slide v-for="(item,index) in banner" :key="index" >
<img :src="item.image" alt="" />
</swiper-slide>
<!-- 常见的小圆点 -->
<div class="swiper-pagination" v-for="(item,index) in banner" :key="index" slot="pagination" ></div>
</swiper>
<!-- 显示数字 -->
<div class="number">{{imgIndex}}/{{detailimages.length}}</div>

  • data中配置

data() {
const that = this;
return {
imgIndex: 1,
swiperOption: {
//是一个组件自有属性,如果notNextTick设置为true,组件则不会通过NextTick来实例化swiper,也就意味着你可以在第一时间获取到swiper对象,假如你需要刚加载遍使用获取swiper对象来做什么事,那么这个属性一定要是true
notNextTick: true,
//循环
loop: true,
//设定初始化时slide的索引
initialSlide: 0,
//自动播放
autoplay: {
delay: 1500,
stopOnLastSlide: false,
/* 触摸滑动后是否继续轮播 */
disableOnInteraction: false
},
//滑动速度
speed: 800,
//滑动方向
direction: "horizontal",
//小手掌抓取滑动
grabCursor: true,
on: {
//滑动之后回调函数
slideChangeTransitionStart: function() {
/* realIndex为滚动到当前的slide索引值 */
that.imgIndex= this.realIndex - 1;
},
},
//分页器设置
pagination: {
el: ".swiper-pagination",
clickable: true,
type: "bullets"
}
}
};
},

3.遇见的问题

  • 这个插件,在图片只有一张时,仍然会自动滚动

这里很需要注意的一点就是,如果你直接设置autoplay为true的话,在你触摸滑动图片后,他就不会再自动滚动了


/* 这里我是在使用接口请求后,对返回的数据进行判断 */
created() {
this.$Request({
url: '',
method: 'get',
success: res => {
this.swiperOption.autoplay = res.result.data.length != 1 ? {
delay: 1500,
stopOnLastSlide: false,
disableOnInteraction: false
} : false;
}
})
}
  • 在on里面,监听slideChangeTransitionStart方法时,在开始的时候,我用的是activeIndex来设置对应的索引值,这个的话,滑向下一张没有发现问题,后面,自己试着滑向上一张图片,就发现出现问题,这个值不对应了,使用realIndex

on: {
slideChangeTransitionStart: function() {
that.imgIndex = this.realIndex + 1;
},
},
  • 在swiper这个容器中,会出现滚动到最后一张图片后就不自动滚动了,以及出现底边的小圆点写了后不显示的问题

原因是因为this.commodity刚开始数据为[],后来赋值才有值,所以要先判断this.commodity是否为空,这里就在swiper这个容器中进行判断,若数据长度为0,就不显示这个容器


<swiper class='swiImgs' :options="swiperOption" v-if="commodity.length!=0"></swiper>

正在努力学习中,若对你的学习有帮助,留下你的印记呗(点个赞咯^_^)

来源:https://segmentfault.com/a/1190000016402768

最新文章

  1. DSY2933*地图
  2. [转]在html中控制自动换行
  3. iOS 杂笔-23(区分各种空值)
  4. /MD, /MT, /LD (Use Run-Time Library)
  5. 低版本IE浏览器 input元素出现叉叉的情况
  6. #ifdef #ifndef使用
  7. Java 集合源码解析(1):Iterator
  8. xml学习总结(二)
  9. UIApplication深入研究
  10. C# in Depth阅读笔记2:C#2特性
  11. leetcode Swap Nodes in Pairs python
  12. mysql优化sql语句的方法
  13. JavaScript的DOM编程--05--获取文本节点
  14. Windows Server 2016-MS服务器应用程序兼容性列表
  15. redis_简单动态字符串
  16. Man方法
  17. 20165306 预备作业3 Linux安装及学习
  18. uri,url和urn的区别以及URLEncoder
  19. E3-1260L (8M Cache, 2.40 GHz) E3-1265L v2 (8M Cache, 2.50 GHz)
  20. OpenWRT使用wifidog实现强制认证的WIFI热点

热门文章

  1. Java 使用模板生成 Word 文件---基于 Freemarker 模板框架
  2. 论文翻译——R-CNN(目标检测开山之作)
  3. [转]WPF焦点概述
  4. wsoj「G2016 SCOI2018 Round #12」建筑师
  5. JS创建和存储 cookie的一些方法
  6. 匿名/局部内部类访问局部变量时,为什么局部变量必须加final
  7. 【MFC 】关于对话框中的OnVScroll() 和 OnHScroll
  8. java异常处理throw和throws的区别
  9. Redis源码解析:27集群(三)主从复制、故障转移
  10. c++设计模式:访问者模式(visitor模式)