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

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

先看看效果:xss

wxml:this

<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 表示前边距和后边距,官网文档有说明的。spa

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

(3) style='height:{{swiperH}}'  这是等比设置swiper高度,由于swiper有固定的高度,因此要动态修改一下。这篇文章也有相似的作法code

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

 

wxss:xml

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这个属性了,有了这个属性才能有第二张图片缩放的效果。htm

 

js:blog

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
    })
},

 

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

相关文章
相关标签/搜索