Vue+swiper异步加载轮播图,并且懒加载

本文介绍如何在Vue项目中利用Swiper组件实现图片轮播效果,包括安装Swiper依赖、配置轮播图样式及代码实现,并详细解释了懒加载功能的设置与使用。
首先要安装swiper 依赖
引入import Swiper from 'swiper'
<div class="swiper-banner">
	            <div class="swiper-wrapper">
	                <div class="swiper-slide" v-for="(str,index) in advertising">
	                	<img src="../../../dist/static/images/ad@2x.png" :data-src="str.imgUrl" class="swiper-lazy">
	                </div>
	            </div>
	            <div class="swiper-pagination">
	            	<span v-for="(str,index) in advertising"></span>
	            </div>
	       </div>
1、
//懒加载--真正要显示的图片放在:data-src="str.imgUrl",loading时放在src="../../../dist/static/images/ad@2x.png"  class定义成swiper-lazy
2、
现在开始mounted时初始化一下
var that = this
that.mySwiper = new Swiper(".swiper-banner",{
        autoplay: 4000,
        loop: that.paginationShow,
        autoplayDisableOnInteraction: false,
        preventLinksPropagation: true,
        lazyLoading : true,//懒加载开启
        pagination: ".swiper-pagination",
        observer:true,//修改swiper自己或子元素时,自动初始化swiper
        observeParents:true,//修改swiper的父元素时,自动初始化swiper
        onSlideChangeEnd: function(swiper){ 
		swiper.update() 
	},
}
3、
然后在你异步请求数据完成后
that.$nextTick(function() {							
	that.mySwiper.startAutoplay()//重新开始轮播
	that.mySwiper.reLoop()//重新计算slides个数
})

参考:http://www.cnblogs.com/DivHao/p/6866399.html
http://www.swiper.com.cn/api/Images/2015/0308/213.html


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值