1 function PhotoDrawImage(ImgD, iwidth, iheight) {
2
3 var image = new Image();
4 image.src = ImgD.src;
5 if (image.width > 0 && image.height > 0) {
6 flag = true;
7 if (image.width / image.height >= iwidth / iheight) {
8 if (image.width > iwidth) {
9 ImgD.style.width = iwidth + "px";
10 ImgD.style.height = (image.height * iwidth) / image.width + "px";
11 } else {
12 ImgD.style.width = image.width + "px";
13 ImgD.style.height = image.height + "px";
14 }
15 }
16 else {
17 if (image.height > iheight) {
18 ImgD.style.height = iheight + "px";
19 ImgD.style.width = (image.width * iheight) / image.height + "px";
20 } else {
21 ImgD.style.width = image.width + "px";
22 ImgD.style.height = image.height + "px";
23 }
24 }
25 //ImgD.style.marginTop = (iheight - ImgD.style.height.split("p")[0]) / 2 + "px";//让图片居中显示
26 }
27 ImgD.style.display = "block";// 我的图片列表是从ajax读取出来的,默认隐藏图片,当高宽计算出来后显示,这样有就不会有图片一闪之后马上变小的的问题。
28 }
调用就很简单了:<img src="图片地址" onload="javascript:PhotoDrawImage(this,280,210)">