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

微信小程序?qū)崿F(xiàn)輪播圖,本例是在*.wpy頁(yè)面開發(fā)(該頁(yè)面的temlate內(nèi)容對(duì)應(yīng)小程序的wxml,style樣式對(duì)應(yīng)小程序的wxss,script對(duì)應(yīng)小程序的js文件,config對(duì)應(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,
//是否顯示畫板指示點(diǎn),根據(jù)圖片數(shù)量自動(dòng)生成多少個(gè)圓點(diǎn)
indicatorDots: true,
//選中點(diǎn)的顏色
//是否豎直
vertical: false,
//是否自動(dòng)切換
autoplay: true,
//自動(dòng)切換的間隔
interval: 3000,
//滑動(dòng)動(dòng)畫時(shí)長(zhǎng)毫秒
duration: 1000,
//所有圖片的高度
imgheights: [],
//圖片寬度
imgwidth: 320,
//默認(rèn)
current: 0
},
imageLoad: function(e) { //獲取圖片真實(shí)寬度
var imgwidth = e.detail.width,
imgheight = e.detail.height,
//寬高比
ratio = imgwidth / imgheight;
console.log(imgwidth, imgheight)
//計(jì)算的高度值
var viewHeight = 750 / ratio;
var imgheight = viewHeight;
var imgheights = this.data.imgheights;
//把每一張圖片的對(duì)應(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>
將代碼粘過(guò)去之后,只需要修改循環(huán)對(duì)象為圖片數(shù)據(jù)就可以了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript實(shí)現(xiàn)點(diǎn)擊改變圖片形狀(transform應(yīng)用)
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)點(diǎn)擊改變圖片形狀,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
javscript 數(shù)組扁平化的實(shí)現(xiàn)
這篇文章主要介紹了javscript 數(shù)組扁平化的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
微信公眾號(hào)h5使用微信支付及支付寶支付的步驟(前端)
微信H5支付是一種支付解決方案,主要適用于商戶在微信客戶端外的移動(dòng)端網(wǎng)頁(yè)上展示商品或服務(wù)的場(chǎng)景,這篇文章主要給大家介紹了關(guān)于微信公眾號(hào)h5使用微信支付及支付寶支付(前端)的相關(guān)資料,需要的朋友可以參考下2024-07-07
JS實(shí)現(xiàn)骰子3D旋轉(zhuǎn)效果
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)骰子3D旋轉(zhuǎn)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10

