js實現圖片等比縮放
//圖片等比例縮放<html>
<head>
<title>等比例縮放圖片</title>
<script>
function DrawImage(ImgD,iwidth,iheight){
//參數(圖片,允許的寬度,允許的高度) var image=new Image(); image.src=ImgD.src; if(image.width>0 && image.height>0){ if(image.width/image.height>= iwidth/iheight){ if(image.width>iwidth){ ImgD.width=iwidth; ImgD.height=(image.height*iwidth)/image.width; }else{ ImgD.width=image.width; ImgD.height=image.height; } }else{ if(image.height>iheight){ ImgD.height=iheight; ImgD.width=(image.width*iheight)/image.height; }else{ ImgD.width=image.width; ImgD.height=image.height; } } }
}
</script>
</head>
<body>
<img src="/images/logo.gif" alt="自動縮放后的效果" width="220" height="80" onload="javascript:DrawImage(this,100,100)" />
</body>
</html></pre>
本文由用戶 efy6 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!