//图片按比例缩放入口,sender为图片框Id function onPreviewLoad(sender){ autoSizePreview( sender, sender.offsetWidth, sender.offsetHeight ); } function autoSizePreview( objPre, originalWidth, originalHeight ){ var zoomParam = clacImgZoomParam( 110, 160, originalWidth, originalHeight ); objPre.style.width = zoomParam.width + 'px'; objPre.style.height = zoomParam.height + 'px'; objPre.style.marginTop = zoomParam.top + 'px'; objPre.style.marginLeft = zoomParam.left + 'px'; } //图片大小比例调整 function clacImgZoomParam( maxWidth, maxHeight, width, height ){ var param = { width:width, height:height, top:0, left:0 }; if( width>maxWidth || height>maxHeight ){ rateWidth = width / maxWidth; rateHeight = height / maxHeight; if( rateWidth > rateHeight ){ param.width = maxWidth; param.height = height / rateWidth; }else{ param.width = width / rateHeight; param.height = maxHeight; } } param.left = (maxWidth - param.width) / 2; param.top = (maxHeight - param.height) / 2; return param; }
//Html标签
this)”/>
本文介绍了一种图片自适应缩放的方法,通过计算图片的缩放参数来确保图片能够在指定区域内按照原始比例显示。该方法适用于前端开发中图片预览等功能。
this)”/>

738

被折叠的 条评论
为什么被折叠?



