如何在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 $router和$route的区别详解
Dec 02 Vue.js
vue-router定义元信息meta操作
Dec 07 Vue.js
详解Vue2的diff算法
Jan 06 Vue.js
基于VUE实现简单的学生信息管理系统
Jan 13 Vue.js
vue实现简易计算器功能
Jan 20 Vue.js
Vue常用API、高级API的相关总结
Feb 02 Vue.js
vue 动态添加的路由页面刷新时失效的原因及解决方案
Feb 26 Vue.js
vue组件的路由高亮问题解决方法
May 11 Vue.js
详解vue中v-for的key唯一性
May 15 Vue.js
vue使用Google Recaptcha验证的实现示例
Aug 23 Vue.js
Element-ui Layout布局(Row和Col组件)的实现
Dec 06 Vue.js
el-table-column 内容不自动换行的解决方法
Aug 14 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
咖啡界又出新概念,无需咖啡豆的分子咖啡
2021/03/03 咖啡文化
PHP调用Twitter的RSS的实现代码
2010/03/10 PHP
那些年我们错过的魔术方法(Magic Methods)
2014/01/14 PHP
PHP实现图片旋转效果实例代码
2014/10/01 PHP
PHP房贷计算器实例代码,等额本息,等额本金
2017/04/01 PHP
JS 强制设为首页的代码
2009/01/31 Javascript
jquery 图片轮换效果
2010/07/29 Javascript
javascript学习笔记(十五) js间歇调用和超时调用
2012/06/20 Javascript
JavaScript对内存分配及管理机制详细解析
2013/11/11 Javascript
javascript弹出页面回传值的方法
2015/01/28 Javascript
ajax如何实现页面局部跳转与结果返回
2015/08/24 Javascript
超详细的javascript数组方法汇总
2015/11/21 Javascript
微信小程序 底部导航栏目开发资料
2016/12/05 Javascript
Vue数据驱动模拟实现3
2017/01/11 Javascript
loading动画特效小结
2017/01/22 Javascript
ES6/JavaScript使用技巧分享
2017/12/14 Javascript
深入理解JS中Number(),parseInt(),parseFloat()三者比较
2018/08/24 Javascript
nodejs 使用 js 模块的方法实例详解
2018/12/04 NodeJs
使用jQuery动态设置单选框的选中效果
2018/12/06 jQuery
详解vue微信网页授权最终解决方案
2019/06/16 Javascript
ES6 let和const定义变量与常量的应用实例分析
2019/06/27 Javascript
vue.js实现h5机器人聊天(测试版)
2020/07/16 Javascript
深入探究Python中变量的拷贝和作用域问题
2015/05/05 Python
Python中shutil模块的学习笔记教程
2017/04/04 Python
tensorflow: variable的值与variable.read_value()的值区别详解
2018/07/30 Python
python将.ppm格式图片转换成.jpg格式文件的方法
2018/10/27 Python
Python笔试面试题小结
2019/09/07 Python
python 密码学示例——凯撒密码的实现
2020/09/21 Python
python 获取字典特定值对应的键的实现
2020/09/29 Python
pytorch下的unsqueeze和squeeze的用法说明
2021/02/06 Python
Rhone官方网站:男士运动服装、健身服装和高级运动服
2019/05/01 全球购物
求职简历的自我评价怎样写好
2013/10/07 职场文书
讲解员培训方案
2014/05/04 职场文书
投标保密承诺书
2014/05/19 职场文书
授权委托书(法人单位用)
2014/09/29 职场文书
配置nginx 重定向到系统维护页面
2021/06/08 Servers