需求背景:
實現圖片輪播功能且高度要自適應。
技術實現思路:
使用小程序自帶組件swiper
。
關鍵點:
就是要計算出當前圖片的高度並賦值給swiper
高度。需要計算是由於swiper
必須指定高度不能像div
一樣自動撐開。
難點:
- 高度自適應失效
- 描述:切換頁面返回 由
onhide
—>onshow
時,出現所有的高度
會保持在最後計算出的那個值,導致高度自適應效果失效。 - 原因:是由於此時
imageLoad
不再監聽。 - 解決辦法:
watch
圖片列表,給url
加參數(時間戳),使其每次都重新加載,使imageLoad
監聽。
- 前後臺切換初始高度不符
- 描述:切換到後臺再返回到前臺時,初始高度會保持出現在第一張圖片的高度,若切換時非第一張圖片,就會導致給當前圖片高度不正確,被遮擋或者有大片空白。
- 原因:給
swiper
賦值的是 圖片列表裏第一張的高度。 - 解決辦法:後臺切回前臺時,
appdata
是保持不變的,而當前圖片排位已被保存變量,所以取當前圖片的高度賦值給swiper
高度。
- 同頁面切換初始高度不符
- 描述:此組件所在頁面,下面有跳轉到當前頁的業務需要,只是渲染數據不同。當返回前一個頁面時,初始高度還保留着返回前最後一次的高度,與當前頁當前圖片高度不符。
- 原因:同頁面切換時,
appdata
沒有重新賦值的話就不會變化,最後當前圖片變量取值了最後出現的那個頁面的當前圖片。 - 解決辦法:每切換到一個頁面時,在圖片組件裏,緩存以頁面數:當前圖片爲鍵值的
currents
對象。返回到某個頁面時,通過當前頁面數取得當前圖片,從而獲得當前初始高度。
PS:
在設計和解決這些難點時,均遵循着組件的高內聚、低耦合原則,使得更具複用性、穩定性、無依賴。
具體實現:
模版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, //默認 (必須)
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">
//高度自適應圖片輪播組件
.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:
很多時候開始發現是未解的,待解決之後發現原來並沒什麼,😄,希望我們在發現問題解決問題的路上結伴而行孜孜不倦~ 有寫的不到之處望能不吝賜教,歡迎隨時交流,共勉~ 😊