1. 程式人生 > >js實現縮圖

js實現縮圖

<script  language="javascript">

//顯示縮圖
function DrawImage(ImgD,width_s,height_s){
/*var width_s=139;
var height_s=104;
*/
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>=width_s/height_s){
if(image.width>width_s){
ImgD.width
=width_s; ImgD.height=(image.height*width_s)/image.width; }else{ ImgD.width=image.width; ImgD.height=image.height; } } else{ if(image.height>height_s){ ImgD.height=height_s; ImgD.width=(image.width*height_s)/image.height; }else{ ImgD.width=image.width; ImgD.height=image.height; } } } /*else{ ImgD.src=""; ImgD.alt="" }*/
} </script> //呼叫 <div><img src="圖片" align="center" onload="DrawImage(this,200,200)"></div>