// 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' />";
}
js自动等比例设置图片尺寸大小
原创
©著作权归作者所有:来自51CTO博客作者清风凌雪的原创作品,请联系作者获取转载授权,否则将追究法律责任

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
JS选择图片获取base64编码预览图片
通过将图片转为data url的base64格式编码,实现直接预览图片
图片预览 base64 dataurl 图片转base64