uniapp解决动态设置swiper轮播图高度的问题(不同图片数组高度不一致,数组内图片高度一致)

功能需求:不同数组的图片高度不一致,数组内图片高度一致的情况下,动态设置swiper高度

尝试过百度的获取图片高度的方法,发现获取高度不准确,后改用获取设备可视宽度和图片高度的方法解决
动态设置swiper高度的问题

原使用不准确代码:

getElementHeight(element) {
				setTimeout(() => {
					let query = uni.createSelectorQuery().in(this);
					query.select(element).boundingClientRect();
					query.exec((res) => {
						if (!res) { //如果没获取到,再调一次
							this.getElementHeight();
						} else {
							this.swiperHeight = res[0].height;
							console.log(this.swiperHeight)
						}
					})
				}, 80)
			},

this.getElementHeight(class类名)

:style="'height:'+swiperHeight+'px'"  //高度动态设置样式给swiper组件

----------------获取到的高度不准确,偶尔出现不同图片高度获取是一样的问题

解决方法:

	uni.getImageInfo({
						src:that.imgIist[0],
						success: function(image) {
							uni.getSystemInfo({
							    success: function (res) {
									console.log(res)
									that.swiperHeight = res.windowWidth/image.width * image.height;
									console.log(that.swiperHeight)
							    }
							});
						}
					});

:style="'height:'+swiperHeight+'px'"  //高度动态设置样式给swiper组件

res.windowWidth:屏幕可视宽度
image.width :uni.getImageInfo获取到的图片宽度
image.height :uni.getImageInfo获取到的图片高度

使用uni.getImageInfo 获取的图片宽高是原始宽高 所以需要与设备可视宽度进行等比例计算

此方法不解决数组内的图片高度不一的情况

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值