超簡單的微信小程序輪播圖
Tips:微信小程序可以在HbuilderX用HTML標簽(如Ddiv、span等)寫前端代碼,也可以用微信小程序語法寫(view、swiper標簽),然后npm run dev編譯后,在微信開發(fā)者工具里面導(dǎo)入該項目,就可以調(diào)試項目,查看效果。
效果圖:

微信小程序?qū)崿F(xiàn)輪播圖,本例是在*.wpy頁面開發(fā)(該頁面的temlate內(nèi)容對應(yīng)小程序的wxml,style樣式對應(yīng)小程序的wxss,script對應(yīng)小程序的js文件,config對應(yīng)小程序的json文件)
<style type="less" scoped="scoped">
.swiper image {
width: 100%;
height: auto;
}
.swiper-image {
height: 100%;
width: 100%;
}
.slide-image {
height: 100%;
width: 100%;
display: block;
}
</style>
<template>
<view class="swiper">
<swiper indicator-dots="{{indicatorDots}}" vertical="{{vertical}}" autoplay="{{autoplay}}" duration="{{duration}}" interval="{{interval}}" indicator-active-color="#007aff" bindchange="bindchange" circular="{{circular}}"
style="height:{{imgheights[current]}}rpx;">
<block wx:for="{{GoodsDatas.imgshow}}" wx:for-key="{{index}}" wx:for-item="image">
<swiper-item>
<image src="{{image.img}}" data-id="{{index}}" class="slide-image" mode="widthFix" bindload="imageLoad" />
</swiper-item>
</block>
</swiper>
</view>
</template>
<script>
import wepy from '@wepy/core'
wepy.page({
data: {
circular: true,
//是否顯示畫板指示點,根據(jù)圖片數(shù)量自動生成多少個圓點
indicatorDots: true,
//選中點的顏色
//是否豎直
vertical: false,
//是否自動切換
autoplay: true,
//自動切換的間隔
interval: 3000,
//滑動動畫時長毫秒
duration: 1000,
//所有圖片的高度
imgheights: [],
//圖片寬度
imgwidth: 320,
//默認
current: 0
},
imageLoad: function(e) { //獲取圖片真實寬度
var imgwidth = e.detail.width,
imgheight = e.detail.height,
//寬高比
ratio = imgwidth / imgheight;
console.log(imgwidth, imgheight)
//計算的高度值
var viewHeight = 750 / ratio;
var imgheight = viewHeight;
var imgheights = this.data.imgheights;
//把每一張圖片的對應(yīng)的高度記錄到數(shù)組里
imgheights[e.target.dataset.id] = imgheight;
this.setData({
imgheights: imgheights
})
},
bindchange: function(e) {
// console.log(e.detail.current)
this.setData({
current: e.detail.current
})
}
})
</script>
將代碼粘過去之后,只需要修改循環(huán)對象為圖片數(shù)據(jù)就可以了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript實現(xiàn)點擊改變圖片形狀(transform應(yīng)用)
這篇文章主要為大家詳細介紹了JavaScript實現(xiàn)點擊改變圖片形狀,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-04-04

