微信小程序之 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
    })
},

 

就這些簡單的代碼就完成啦 ^_^

相關文章
相關標籤/搜索