uniapp 小程序实现图片宽度100%、高度自适应的效果

因为image组件默认是有宽度跟高度的,所以这个高度不怎么好写

通过@load事件来控制图片的高度

话不多说,直接上代码,

html 复制代码
<image 
	class="img" 
	src="@/static/image.png" 
	:style="{ height: imgHeight + 'px' }"
	mode="widthFix" 
	@load="onImgLoad">
</image>
javascript 复制代码
data() {
	return {
		imgHeight: 0
	}
},
methods: {
	 onImgLoad(e) {
	     // 当图片加载完成后,获取图片的原始宽度和高度,并根据宽度计算出高度
	    const { width, height } = e.mp.detail;
	     this.imgHeight = (height / width) * 100; // 高度 = 原始高度 / 原始宽度 * 100
	},
},
css 复制代码
.img{
	width: 100%;
}

随手一记~

相关推荐
sumAll3 分钟前
拒绝黑盒!Spring @Scheduled 定时任务源码深度解析
java·后端·spring
Seven974 分钟前
剑指offer-47、求1+2+3...+n
java
ZePingPingZe5 分钟前
Spring boot2.x-第05讲番外篇:常用端点说明
java·spring boot·后端
Macbethad6 分钟前
WPF 工业设备管理程序技术方案
java·大数据·hadoop
Hello.Reader8 分钟前
Flink SQL 窗口函数从 OVER 到 TopN 的完整套路
java·sql·flink
她说彩礼65万10 分钟前
C# ConcurrentDictionary详解
java·服务器·c#
Han.miracle13 分钟前
Maven 基础与 Spring Boot 入门:环境搭建、项目开发及常见问题排查
java·spring boot·后端
特拉熊14 分钟前
23种设计模式之桥接模式
java·架构
半瓶榴莲奶^_^14 分钟前
后端Web进阶(AOP)
java·开发语言