Posted in Javascript onMay 13, 2010
var scaleImage = function(o, w, h){ var img = new Image(); img.src = o.src; if(img.width >0 && img.height>0) { if(img.width/img.height >= w/h) { if(img.width > w) { o.width = w; o.height = (img.height*w) / img.width; } else { o.width = img.width; o.height = img.height; } o.alt = img.width + "x" + img.height; } else { if(img.height > h) { o.height = h; o.width = (img.width * h) / img.height; } else { o.width = img.width; o.height = img.height; } o.alt = img.width + "x" + img.height; } } }
HTML
<div> <p><img src="../comment/img/max.jpg" onload="scaleImage(this,300,300)" alt="pic" /></p> <p><img src="../comment/img/2.jpg" onload="scaleImage(this,300,300)" /></p> <p><img src="../comment/img/3.jpg" onload="scaleImage(this,300,300)" /></p> <p><img src="../comment/img/4.jpg" onload="scaleImage(this,300,300)" /></p> </div>
js 图片等比例缩放代码
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@