微信小程序 实现图片自适应

< image src= "/images/1.jpg" bindload= "imageLoad"
style= "width:{{imgwidth}}rpx; height:{{imgheight }}rpx;"></ image >

imageLoad: function (e) {
var _this = this;
var $width = e.detail.width, //获取图片真实宽度
$height = e.detail.height,
ratio = $width / $height; //图片的真实宽高比例
var viewWidth = 800, //设置图片显示宽度,
viewHeight = 800 / ratio; //计算的高度值
this.setData({
imgwidth: viewWidth,
imgheight: viewHeight
})
}

猜你喜欢

转载自blog.csdn.net/qq_37902065/article/details/80340755