setImage(i) {
this.$nextTick(() => { //确保渲染完成后再获取数据
let image = new Image()
let Img = document.getElementsByTagName('img').namedItem('img' + this.addTemp.tid + i)//根据图片的name属性获取图片
Img.width = 0
Img.height = 0
image.src = Img.src
let timer = setInterval(() => {//定时器保证图片加载完成,否则获取的图片宽高可能为0
if (image.width == 0 && image.height == 0) {
return
} else {
if (image.width < 148 && image.height < 148) {
Img.width = image.width;
Img.height = image.height;
} else {
if (1 <= image.width / image.height) //原图片宽高比例 大于 图片框宽高比例
{
Img.width = 148; //以框的宽度为标准
Img.height = 148 * (image.height / image.width);
}
else { //原图片宽高比例 小于 图片框宽高比例
Img.width = 148 * (image.width / image.height);
Img.height = 148; //以框的高度为标准
}
}
clearTimeout(timer)
}
}, 200)
})
},
定时器获取图片宽高那里应该也可以用 image.onload事件来保证获取正确的宽高,这个我没有试,大家可以自己试一下。