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

微信小程序實現輪播圖,本例是在*.wpy頁面開發(fā)(該頁面的temlate內容對應小程序的wxml,style樣式對應小程序的wxss,script對應小程序的js文件,config對應小程序的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,
//是否顯示畫板指示點,根據圖片數量自動生成多少個圓點
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;
//把每一張圖片的對應的高度記錄到數組里
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)對象為圖片數據就可以了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
pace.js和NProgress.js兩個加載進度插件的一點小總結
這兩個插件都是關于加載進度動畫的,今天就和大家一起了解下pace.js和NProgress.js兩個加載進度插件的一點小總結,感興趣的朋友一起看看吧2018-01-01
詳解微信圖片防盜鏈“此圖片來自微信公眾平臺 未經允許不得引用”的解決方案
這篇文章主要介紹了詳解微信圖片防盜鏈“此圖片來自微信公眾平臺 未經允許不得引用”的解決方案,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04
innerHTML 和 getElementsByName 在IE下面的bug 的解決
innerHTML 真的一個麻煩的東西。IE 和 firefox 對dom 處理的方式不是很一樣。IE 對動態(tài)加載的很多dom 不支持動態(tài)更新。2010-04-04

