可隨螢幕自動縮放的圖片
原始圖檔 pic01.jpg 尺寸 600 X 450
這是原始大圖片
使用 JavaScript 程式將此圖檔 pic01.jpg 自動縮放尺寸為 300 X 225
這是自動縮放後的圖片效果
使用 JavaScript 程式如下:
<script language="JavaScript" type="text/javascript">
<!--
function DrawImage(ImgD,FitWidth,FitHeight){
	var image=new Image();
	image.src=ImgD.src;
	if(image.width>0 && image.height>0){
		if(image.width/image.height>= FitWidth/FitHeight){
			if(image.width>FitWidth){
				ImgD.width=FitWidth;
				ImgD.height=(image.height*FitWidth)/image.width;
			}else{
				ImgD.width=image.width;
				ImgD.height=image.height;
			}
		} else{
			if(image.height>FitHeight){
				ImgD.height=FitHeight;
				ImgD.width=(image.width*FitHeight)/image.height;
			}else{
				ImgD.width=image.width;
				ImgD.height=image.height;
			}
		}
	}
}
//-->
</script>
<img src=pics/pic01.jpg border=0 onload="javascript:DrawImage(this,300,225);" alt="這是自動縮放後的圖片效果">
本報導資料來源:夢想空間電腦資源討論區

請按瀏覽器的檢視原始碼查看