微信小程序-横向轮播时,轮播高度随内容高度动态撑开

在使用小程序轮播图过程中,发现轮播滑动高度是固定的,当活动到第二页时,会出现很大一块空白内容,视觉效果比较差
实现效果:

第一页:

第二页:

轮播高度计算

复制代码
每页轮播高度 = 当页图片总高度 + 总间距

图片设计图显示尺寸(px):105 × 108
设计图标准尺寸(px):375
屏幕宽度(px):screenWidth
屏幕与设计图分辨率比率: ratio = screenWidth / 375
图片实际展示尺寸:imgWidth = 105 * ratio,  imgHeight = 108 * ratio

已知图片间距 space = 12 px
每页轮播图片展示行数 row

图片总高度 = imgHeight * row
总间距 = (row - 1) * 12px

① 获取screenWidth,计算屏幕与设计图分辨率比率 ratio

javascript 复制代码
let windowInfo = wx.getWindowInfo()
let ratio = windowInfo.screenWidth / 375
this.setData({
	ratio: ratio
})

② 计算轮播图片展示行数 row,设置首页轮播高度swiperHeight

javascript 复制代码
let imgHeight = 108
let row = Math.ceil(this.data.list[0].length / 3) // 计算单页轮播行数 
let swiperHeight = row * imgHeight *  +  (row - 1) * 12
this.setData({
	currentSwiper: 0, // 默认轮播下标
	swiperHeight: swiperHeight // 首页轮播高度
})
html 复制代码
<swiper 
	class="my-swipe" 
	indicator-dots="{{false}}" 
	interval="{{5000}}"
	style="height: {{swiperHeight}}px;"
	current="{{currentSwiper}}"
	bindchange="onChange"
>
</swiper>

③ 轮播图滑动时,重新计算高度

javascript 复制代码
onChange() {
	let currentIndex = el.detail.current
	let row = Math.ceil((this.data.list[currentIndex].length / 3))
	let swiperHeight = row * 108 * this.data.ratio + (12 * (row - 1))
	this.setData({
		swiperHeight: swiperHeight
	})
}
相关推荐
万岳科技系统开发10 小时前
校园跑腿外卖搭建助力高校周边商家拓展线上业务
大数据·人工智能·小程序
微三云 - 廖会灵 (私域系统开发)17 小时前
消费返物业费平台搭建:美家时代商业模式拆解与系统开发选型指南
小程序·系统架构
greenbbLV20 小时前
中小公司积分商城选型:SaaS与私有化方案对比解析
大数据·前端·小程序
2501_915909062 天前
iOS 应用反调试技详解术 检测调试器的原理与防护实践
android·ios·小程序·https·uni-app·iphone·webview
00后程序员张2 天前
iOS加固技术路线全面解析:Bitcode模式、源码模式与汇编模式对比及爱加密优势
android·汇编·ios·小程序·uni-app·cocoa·iphone
海纳百川·纳海川2 天前
租房行业数字化:换个思路解决“老问题”
大数据·微信小程序·小程序
上海云盾-小余3 天前
CC 高频请求 + DDoS 流量攻击实战防御:全链路 WAF 流量清洗部署完整流程
网络·爬虫·安全·小程序·ddos
2501_916008893 天前
iOS IPA文件反编译与打包操作方法,拆包分析防护和加固打包
android·macos·ios·小程序·uni-app·cocoa·iphone
2301_810313183 天前
海外教育APP定制发展现状与开发选型核心要点解析
小程序·团队开发·软件需求
2501_915921433 天前
iOS手游安全反外挂解决方案:基于Xcode的加固混淆特色详解
android·安全·ios·小程序·uni-app·iphone·xcode