如何在VUE中使用vue-awesome-swiper


Posted in Vue.js onJanuary 04, 2021

一:首先进入项目目录中安装

install vue-awesome-swiper@2.6.7 --save

二.使用

全局挂载:

import Vue from 'vue'
import VueAwesomeSwiper from 'vue-awesome-swiper'
// require styles
import 'swiper/dist/css/swiper.css'

Vue.use(VueAwesomeSwiper, /* { default global options } */)

组件挂载

// require styles
import 'swiper/dist/css/swiper.css'
import { swiper, swiperSlide } from 'vue-awesome-swiper'
export default {
 components: {
 swiper,
 swiperSlide
 }
}

新建一个.vue文件,按照以下代码模式

<template>
 <div class="index">
 <Top navLeft="true" title="轮播图" navRight="false"></Top>
  <div style="padding-top: 1.3rem;padding-left:0.2rem">
   <swiper id="mySwiper" :options="swiperOption" ref="mySwiper" v-if="swiperList.length!=0">
   <swiper-slide class="swiper-item" v-for='(item,index) of swiperList' :key='item.id' >
     <img class='swiper-img' :src='item.imgUrl' alt="门票" @click="swiperClick(index,item.linkUrl)" />
   </swiper-slide>
   <div class="swiper-pagination" slot="pagination"></div>
  </swiper>
 </div>
 </div>
</template>
<script>
import Top from '@/components/public/Top';
import 'swiper/dist/css/swiper.css';
import { swiper, swiperSlide } from 'vue-awesome-swiper'
export default {
 name: 'Swiper',
 components: {Top,swiper,swiperSlide},
 data() {
  return {
  swiperList:[],
  swiperOption: {
   pagination: ".swiper-pagination",
   initialSlide: 0,//默认第几张
   loop:true, //循环
   autoplayDisableOnInteraction:false,//触摸后再次自动轮播
   autoplay:2000, //每张播放时长3秒,自动播放
   speed:1000,//滑动速度
  }
  }
 },
 created(){
 this.initEvent(); 
 console.log(this.$refs.mySwiper);
 this.swiperOption.autoplay = this.swiperList.length != 1 ? 2000 : false;//如果是一张图片不轮播

 
 },
 
 computed: {
  swiper() {
   return this.$refs.mySwiper.swiper//组件实例
  }
 },
 mounted(){ 
  
 },
 methods: {
 initEvent:function(){
  this.$http.get("http://localhost/swiper")
  .then(res=>{
   this.swiperList=res.data.swiperList;
  })
  .catch(error=>{
   console.log(error)
  })
 },
 swiperClick:function(index,url){
  console.log(index);
  this.$router.push(url)
 }
 }
}
</script>

<style scoped>
@import "../assets/public/css/bottom.css";
@import "../assets/css/index/my.css";
#mySwiper >>> .swiper-pagination-bullet {
 background: #000000;
}
#mySwiper >>> .swiper-pagination-bullet-active {
 background: #ff0000;
}
</style>

页面展示如下

如何在VUE中使用vue-awesome-swiper

三:在使用过程中遇到的问题

1.触摸后再次自动轮播问题,添加以下属性就可以再次轮播

autoplayDisableOnInteraction:false

2.样式穿透问题,修改圆点的样式问题

  解决方案是给swiper容器添加上ID,然后再在后面添加>>>,这样就可以解决了

#mySwiper >>> .swiper-pagination-bullet {
 background: #000000;
}
#mySwiper >>> .swiper-pagination-bullet-active {
 background: #ff0000;
}

3.解决如果只有一张图片不轮播问题

以上就是如何在VUE中使用vue-awesome-swiper的详细内容,更多关于VUE中使用vue-awesome-swiper的资料请关注三水点靠木其它相关文章!

Vue.js 相关文章推荐
vue+element_ui上传文件,并传递额外参数操作
Dec 05 Vue.js
vue中如何添加百度统计代码
Dec 19 Vue.js
基于Vue2实现移动端图片上传、压缩、拖拽排序、拖拽删除功能
Jan 05 Vue.js
详解vue之自行实现派发与广播(dispatch与broadcast)
Jan 19 Vue.js
如何使用RoughViz可视化Vue.js中的草绘图表
Jan 30 Vue.js
vue使用lodop打印控件实现浏览器兼容打印的方法
Feb 07 Vue.js
vue前端工程的搭建
Mar 31 Vue.js
详解Vue的options
May 15 Vue.js
vue实现锚点定位功能
Jun 29 Vue.js
vue整合百度地图显示指定地点信息
Apr 06 Vue.js
vue3不同环境下实现配置代理
May 25 Vue.js
vue如何清除浏览器历史栈
May 25 Vue.js
vue项目如何监听localStorage或sessionStorage的变化
Jan 04 #Vue.js
手写Vue源码之数据劫持示例详解
Jan 04 #Vue.js
vue+vant 上传图片需要注意的地方
Jan 03 #Vue.js
vue调用微信JSDK 扫一扫,相册等需要注意的事项
Jan 03 #Vue.js
vue中使用echarts的示例
Jan 03 #Vue.js
vue 动态生成拓扑图的示例
Jan 03 #Vue.js
Vue中强制组件重新渲染的正确方法
Jan 03 #Vue.js
You might like
配置PHP使之能同时支持GIF和JPEG
2006/10/09 PHP
php $_ENV为空的原因分析
2009/06/01 PHP
PHP通过header实现文本文件下载的代码
2010/08/08 PHP
php使用array_rand()函数从数组中随机选择一个或多个元素
2014/04/28 PHP
Lumen timezone 时区设置方法(慢了8个小时)
2018/01/20 PHP
yii2 开发api接口时优雅的处理全局异常的方法
2019/05/14 PHP
jquery.messager.js插件导致页面抖动的解决方法
2013/07/14 Javascript
取消选中单选框radio的三种方式示例介绍
2013/12/23 Javascript
jQuery实现tag便签去重效果的方法
2015/01/20 Javascript
jQuery实现右下角可缩放大小的层完整实例
2016/06/20 Javascript
Jquery组件easyUi实现表单验证示例
2016/08/23 Javascript
JavaScript编码风格指南(中文版)
2016/08/26 Javascript
jQuery树形插件jquery.simpleTree.js用法分析
2016/09/05 Javascript
针对后台列表table拖拽比较实用的jquery拖动排序
2016/10/10 Javascript
利用angular.copy取消变量的双向绑定与解析
2016/11/25 Javascript
VUE2 前端实现 静态二级省市联动选择select的示例
2018/02/09 Javascript
javascript填充默认头像方法
2018/02/22 Javascript
layui 解决富文本框form表单提交为空的问题
2019/10/26 Javascript
CentOS中使用virtualenv搭建python3环境
2015/06/08 Python
python随机取list中的元素方法
2018/04/08 Python
Flask框架信号用法实例分析
2018/07/24 Python
查看python下OpenCV版本的方法
2018/08/03 Python
python 利用for循环 保存多个图像或者文件的实例
2018/11/09 Python
python实现生成字符串大小写字母和数字的各种组合
2019/01/01 Python
python实现五子棋程序
2020/04/24 Python
利用纯CSS3实现tab选项卡切换示例代码
2016/09/21 HTML / CSS
一篇文章带你学习CSS3图片边框
2020/11/04 HTML / CSS
英国最大的割草机购买网站:Just Lawnmowers
2019/11/02 全球购物
《老山界》教学反思
2014/04/08 职场文书
家长通知书家长评语
2014/04/17 职场文书
社区巾帼文明岗事迹材料
2014/06/03 职场文书
工商管理专业自荐信
2014/06/03 职场文书
企业趣味活动方案
2014/08/21 职场文书
我们的节日重阳节活动总结
2015/03/24 职场文书
大学生入党自传2015
2015/06/26 职场文书
MySQL之PXC集群搭建的方法步骤
2021/05/25 MySQL