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>
页面展示如下
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的资料请关注站长技术其它相关文章!
初识C语言 本章节的笔记只是对C语言的基础知识的一个小概括 详细的讲解笔记后续...
科技的变革,带来新的产品和技术,也带给我们全新的数字生活方式。 爱范儿旗下的...
SpringMVC下获取验证码实例详解 前言: 1.用户一开始登录的时候, 不建议出现验证...
前言 本篇博客中,我将描述如何在ASP.NET Core程序启动时,确保强类型配置对象正...
下面一段简单的代码给大家介绍正则表达式截取身份证号码加密的方法,具体代码如...
Git 标签 如果你达到一个重要的阶段,并希望永远记住那个特别的提交快照,你可以...
关于如题这几个概念,大部分人应该比较熟悉,但是我昏了好久,所以写下来加深印...
1、问题:网关解析正确,我.net这边解析错误,原因是,本来是无符号的,我搞成了...
目录 实验目的 第1关汉字字库存储芯片扩展实验 第2关MIPS寄存器文件设计 第3关MI...
企业微信端产品“C端用户小程序”,是一款服务于vivo线下代理、门店和导购,帮助...