让图片算好高宽再显示

var ctn = document.getElementById('img');
var imgO = new Image();
imgO.onload = function(){
  if(imgO.width>400){
    this.width = 400;
     //在这里也可设置高度
  }
   //图片下载完毕了,高宽设好了,再添加到dom结构中去
  ctn.appendChild(imgO);
}
imgO.src = url;
原文地址:https://www.cnblogs.com/tyhmj/p/2288163.html