您的位置:首页 > 移动开发 > 微信开发

微信小程序之 3d轮播(swiper来实现)

2018-11-28 11:11 183 查看

以前写过一篇3d轮播,就是这篇,使用的方法比较笨拙,而且代码不简洁。这次发现swiper也能实现同样的效果。故记录一下。

先看看效果:

wxml:

<swiper previous-margin='50px'  next-margin='50px' bindchange="swiperChange" style='height:{{swiperH}};'>
<swiper-item wx:for='{{imgList}}' wx:key=''>
<image class='le-img {{nowIdx==index?"le-active":""}}' bindload='getHeight' src='{{item}}' style='height:{{swiperH}};'></image>
</swiper-item>
</swiper>

(1) previous-margin 和 next-margin 表示前边距和后边距,官网文档有说明的。

(2) swiperChange 就是swiper的切换事件名

(3) style='height:{{swiperH}}'  这是等比设置swiper高度,因为swiper有固定的高度,所以要动态修改一下。这篇文章也有类似的做法

(4) getHeight 是获取图片的宽高,然后再去设置高度这样才能让图片等比缩放

 

wxss:

swiper {
padding-top: 30px;
}
.le-img {
width: 100%;
display: block;
transform: scale(0.8);
transition: all 0.3s ease;
border-radius: 6px;
}
.le-img.le-active {
transform: scale(1);
}

(1) 最主要的就是scale这个属性了,有了这个属性才能有第二张图片缩放的效果。

 

js:

data: {
swiperH:'',//swiper高度
nowIdx:0,//当前swiper索引
imgList:[//图片列表
"/public/img/idx-ad.png",
"/public/img/idx-ad.png",
"/public/img/idx-ad.png",
]
},
//获取swiper高度
getHeight:function(e){
var winWid = wx.getSystemInfoSync().windowWidth - 2*50;//获取当前屏幕的宽度
var imgh = e.detail.height;//图片高度
var imgw = e.detail.width;
var sH = winWid * imgh / imgw + "px"
this.setData({
swiperH: sH//设置高度
})
},
//swiper滑动事件
swiperChange:function(e){
this.setData({
nowIdx: e.detail.current
})
},

 

就这些简单的代码就完成啦 ^_^

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: