需求背景:
實現(xiàn)圖片輪播功能且高度要自適應(yīng)。
技術(shù)實現(xiàn)思路:
使用小程序自帶組件swiper
。
關(guān)鍵點:
就是要計算出當(dāng)前圖片的高度并賦值給swiper
高度。需要計算是由于swiper
必須指定高度不能像div
一樣自動撐開妒穴。
難點:
- 高度自適應(yīng)失效
- 描述:切換頁面返回 由
onhide
—>onshow
時,出現(xiàn)所有的高度
會保持在最后計算出的那個值摊崭,導(dǎo)致高度自適應(yīng)效果失效讼油。 - 原因:是由于此時
imageLoad
不再監(jiān)聽。 - 解決辦法:
watch
圖片列表呢簸,給url
加參數(shù)(時間戳)矮台,使其每次都重新加載,使imageLoad
監(jiān)聽根时。
- 前后臺切換初始高度不符
- 描述:切換到后臺再返回到前臺時瘦赫,初始高度會保持出現(xiàn)在第一張圖片的高度,若切換時非第一張圖片蛤迎,就會導(dǎo)致給當(dāng)前圖片高度不正確确虱,被遮擋或者有大片空白。
- 原因:給
swiper
賦值的是 圖片列表里第一張的高度替裆。 - 解決辦法:后臺切回前臺時校辩,
appdata
是保持不變的,而當(dāng)前圖片排位已被保存變量辆童,所以取當(dāng)前圖片的高度賦值給swiper
高度宜咒。
- 同頁面切換初始高度不符
- 描述:此組件所在頁面,下面有跳轉(zhuǎn)到當(dāng)前頁的業(yè)務(wù)需要把鉴,只是渲染數(shù)據(jù)不同故黑。當(dāng)返回前一個頁面時,初始高度還保留著返回前最后一次的高度,與當(dāng)前頁當(dāng)前圖片高度不符场晶。
- 原因:同頁面切換時混埠,
appdata
沒有重新賦值的話就不會變化,最后當(dāng)前圖片變量取值了最后出現(xiàn)的那個頁面的當(dāng)前圖片诗轻。 - 解決辦法:每切換到一個頁面時岔冀,在圖片組件里,緩存以頁面數(shù):當(dāng)前圖片為鍵值的
currents
對象概耻。返回到某個頁面時,通過當(dāng)前頁面數(shù)取得當(dāng)前圖片罐呼,從而獲得當(dāng)前初始高度鞠柄。
PS:
在設(shè)計和解決這些難點時,均遵循著組件的高內(nèi)聚嫉柴、低耦合原則厌杜,使得更具復(fù)用性、穩(wěn)定性计螺、無依賴夯尽。
具體實現(xiàn):
模版template:
<template>
<view class="com_imagesSwiper">
<swiper class="com_img_auto_swiper" indicator-dots="true" autoplay="{{false}}" circular="true" bindchange="bindchange" style="height:{{swiperHeight}}px;">
<block wx:for="{{imgUrls}}" wx:key="{{index}}">
<swiper-item>
<image src="{{item}}" class="com_swiper_image" mode="widthFix" data-id='{{index}}' bindload="imageLoad"/>
</swiper-item>
</block>
</swiper>
</view>
</template>
腳本script:
<script>
import wepy from 'wepy'
export default class ImgSwiper extends wepy.component{
props = {
imgUrls: Array
}
data = {
// imgUrls : [
// // '/m/static/img/weixin/act_hongbao.png'
// ],
imgheights: [], //所有圖片的高度 (必須)
imgwidth: 750, //圖片寬度 兼容
current: 0, //默認(rèn) (必須)
swiperHeight: 150,
currentPage: 1
};
watch = {
imgUrls(newValue, oldValue) {
if (newValue) {
for (let i = 0; i < newValue.length; i++) {
newValue[i] = newValue[i] + '?' + new Date().getTime()
}
}
},
currentPage(newValue, oldValue) {
this.current = JSON.parse(wepy.getStorageSync('currents'))[this.getCurrentPages().length] - 0
this.$apply()
},
current(newValue, oldValue) {
let key = this.getCurrentPages().length+''
let obj = JSON.parse(wepy.getStorageSync('currents'))
obj[key] = this.current
wepy.setStorageSync('currents', JSON.stringify(obj))
}
};
methods = {
imageLoad: function (e) {//獲取圖片真實寬度
this.currentPage = this.getCurrentPages().length
let imgwidth = e.detail.width,
imgheight = e.detail.height,
ratio = imgwidth / imgheight;
let viewHeight = this.imgwidth / ratio;
this.imgheights[e.target.dataset.id] = viewHeight;
this.swiperHeight = this.imgheights[this.current]
this.$apply()
},
bindchange: function (e) {
this.current = e.detail.current
this.swiperHeight = this.imgheights[e.detail.current]
this.$apply()
}
}
onLoad() {
this.imgwidth = wx.getSystemInfoSync().screenWidth
this.$apply()
if (!wepy.getStorageSync('currents')) {
wepy.setStorageSync('currents', JSON.stringify({1 : 0}))
} else {
let obj = JSON.parse(wepy.getStorageSync('currents'))
obj[this.getCurrentPages().length] = 0
wepy.setStorageSync('currents', JSON.stringify(obj))
}
}
}
</script>
樣式style:
<style lang="less">
//高度自適應(yīng)圖片輪播組件
.com_imagesSwiper {
.com_img_auto_swiper {
transition: height 0.5s ease;
.com_swiper_image {
width: 100%;
height: 100%;
}
.wx-swiper-dot {
width: 20rpx;
display: inline-flex;
height: 2rpx;
justify-content: space-between;
}
.wx-swiper-dot::before {
content: '';
flex-grow: 1;
background: rgba(255, 255, 255, 0.6);
border-radius: 8rpx
}
.wx-swiper-dot-active::before {
background: rgba(255, 255, 255, 1);
}
}
}
</style>
Notes:
很多時候開始發(fā)現(xiàn)是未解的,待解決之后發(fā)現(xiàn)原來并沒什么登馒,??匙握,希望我們在發(fā)現(xiàn)問題解決問題的路上結(jié)伴而行孜孜不倦~ 有寫的不到之處望能不吝賜教,歡迎隨時交流陈轿,共勉~ ??