先定义一下图片的样式:主要应用CSS的表达式expression方法,一般高版本或IE浏览器有保证;
img {max-width:340px;CURSOR: hand;width:expression(onreadystatechange=function(){if(this.width>340)this.width=340;});ttt:expression(onclick=function(){window.open(this.src)})}
接着定义JS:
<!--
var flag=false;
function DrawImage(ImgD,FitWidth,FitHeight){
var image=new Image();
image.src=ImgD.src;
alert(image.width);
if(image.width>0 && image.height>0){
flag=true;
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;
}
ImgD.alt="点击图片放大("+image.width+"×"+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;
}
ImgD.alt="点击图片放大("+image.width+"×"+image.height+")";
}
}
}
//-->
在图片上应用调用:javascript:DrawImage(this,宽,高)
<img src="郑州网建.jpg" onload="javascript:DrawImage(this,450,325);" border="0" id="inp" style="cursor:hand">