<SCRIPT LANGUAGE="JavaScript">
<!--
var iOriginalWidth, iOriginalHeight; //存放图片原始长度、宽度
//动态显示图片的长度、宽度和显示比例
function dispCurrentSize()
{
var zoom = document.all.idImg.style.zoom;
zoom = eval(zoom.substring(0, zoom.length-1));
var iWidth = zoom * document.all.idImg.width;
var iHeight = zoom * document.all.idImg.height;
}
//显示图片原始长度、宽度
function dispOriginalSize(oImg,x,y)
{
iOriginalWidth = oImg.width;
iOriginalHeight = oImg.height;
if (iOriginalWidth > x || iOriginalHeight > y )
{
oImg.width = x;
oImg.height = oImg.height*(x/oImg.width)
if ( oImg.height > y)
{
iOriginalHeight = oImg.height
oImg.height = y;
oImg.width = oImg.width*(y/iOriginalHeight);
}
}
document.all.idImg.style.zoom = "100%";
dispCurrentSize();
}
//-->
</SCRIPT>
<img src="http://www.seed-china.com/20051231news.jpg" width="150" border="0" ID="idImg"
onLoad="dispOriginalSize(this,150,180);">
下面是在网上看到的
JS代码(随便放哪里):
<script language="JavaScript">
<!--
var flag=false;
function DrawImage(ImgD){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= 180/110){
if(image.width>180){
ImgD.width=180;
ImgD.height=(image.height*110)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
/*ImgD.alt="bigpic" */
}
else{
if(image.height>110){
ImgD.height=110;
ImgD.width=(image.width*110)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
/*ImgD.alt="bigpic" */
}
}
}
//-->
</script>
图片使用的地方:
<img src="图片" border=0 width="180" height="110" onload="javascript drawImage(this);">
width="180" height="110" 注意这里最好限定,如果不限定加载图时会成原大,然后再缩小,这个过程如果图大了很难看的.这里是宽度和高度,在前面的JS里改,这里也作相应的改.
还有一种方法(没试过,呵呵。有兴趣的试下):
style=table-layout:fixed;word-break:break-all