&#106avascript实现图片按比例缩放的函数

王朝html/css/js·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

图片按比例缩放函数:

<script language="JavaScript">

<!--

//图片按比例缩放

var flag=false;

function DrawImage(ImgD){

var image=new Image();

var iwidth = 150; //定义允许图片宽度

var iheight = 100; //定义允许图片高度

image.src=ImgD.src;

if(image.width>0 && image.height>0){

flag=true;

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;

}

ImgD.alt=image.width+"×"+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;

}

ImgD.alt=image.width+"×"+image.height;

}

}

}

//调用:<img src="图片" onload="javascript:DrawImage(this)">

//-->

</script>

示例如下:

<script language="JavaScript">

<!--

//图片按比例缩放

var flag=false;

function DrawImage(ImgD){

var image=new Image();

var iwidth = 150; //定义允许图片宽度

var iheight = 100; //定义允许图片高度

image.src=ImgD.src;

if(image.width>0 && image.height>0){

flag=true;

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;

}

ImgD.alt=image.width+"×"+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;

}

ImgD.alt=image.width+"×"+image.height;

}

}

}

//调用:<img src="图片" onload="javascript:DrawImage(this)">

//-->

</script>

<img src="http://www.webjx.com/img/200406301.jpg" onload="javascript:DrawImage(this)">

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航