// Img是html中的<img>元素,maxWidth最大宽度,maxHeight最大高度function imgAutoSize(Img, maxWidth, maxHeight) { var img = new Image(); img.src = imgData.attr("src"); var imgHeight = img.height; // 原图片大小 var imgWidth = img.width; //原图片大小 // 如果图片高度或者宽度大于限定的高度或者宽度则进行等比例尺寸压缩 if (imgHeight > winHeight || imgWidth > winWidth) { // 1.原图片宽高比例 大于等于 图片框宽高比例 if (winWidth/ winHeight <= imgWidth / imgHeight) { imgWidth = winWidth; //以框的宽度为标准 imgHeight = winWidth * (imgHeight / imgWidth); } // 2.原图片宽高比例 小于 图片框宽高比例 if (winWidth/ winHeight > imgWidth / imgHeight) { imgWidth = winHeight * (imgWidth / imgHeight); imgHeight = winHeight ; //以框的高度为标准 } } var imgHtml = "<img src='" + img.src + "' width='" + width + "px' height='" + height + "px' />"; }