uview 轮播图高度自适应

因为uview轮播的高度是number高度,放大后无法自适应

你肯定想说 可以这样写啊

:height="swiperHeightOne"

swiperHeightOne: uni.upx2px(426)

但是不行,放大后还是会这样噢, 因为最外面控制了最大宽度

最好的处理办法:

<u-swiper class="listBackGSwiper" :height="swiperHeightOne" :list="formData.banner" indicator indicatorMode="dot" circular :interval="5000" img-mode="widthFix"></u-swiper>

swiperHeightOne: uni.upx2px(426)  // 默认值

this.formData.banner = ["b.jpg","c.jpg","a.jpg"]
// 调用方法获取图片的高度
this.getImageList(this.formData.banner).then(res_h=>{
	console.log(res_h)
	const maxNum = Math.max(...res_h)
	this.swiperHeightOne = maxNum
})

//处理图片高度方法  (由于uni.getImageInfo是异步,所以用promise,同时在小程序里面需要配置白名单)
			async getImageList(imageList, w=750) {
				const hieghtList = [],
					getImageInfoSync = (src) => {
						return new Promise((resolve, reject) => {
							uni.getImageInfo({
								src,
								success(r) {
									console.log('长宽',r.width, r.height)
// 获取到最大宽度的div的宽度
									let info = uni.createSelectorQuery().select('.con-center-top').fields({
										size: true
									}, (res) => {
										const hieght = res.width / r.width * r.height
										// 得到最大父级div的宽度 再得到轮播图应该的高度
										resolve(hieght)
									}).exec()
									
								},
	
							})
						})
					}
				for (let item of imageList) {
					const [height] = await getImageInfoSync(item).then(r => [r, null], f => [null, f])
					hieghtList.push(height)
				}
				return Promise.resolve(hieghtList) 
			}

完美解决~~

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值