wangtianbao

等比例改变图片大小

function imgSize(ImgD, iwidth, iheight) {
 var image = new Image();
 image.src = ImgD.src;
 if (image.width > 0 && image.height > 0) {
  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;
   }
  } else {
   if (image.height > iheight) {
    ImgD.height = iheight;
    ImgD.width = (image.width * iheight) / image.height;
   } else {
    ImgD.width = image.width;
    ImgD.height = image.height;
   }
  }
 }
}

function changeAllImage(iwidth, iheight){
 var imgs = document.images;
 for (j=0; j<imgs.length; j=j+1){
  var img = document.images[j];
  imgSize(img, iwidth, iheight);
 }
}


=======================

引用:
<img src='001.jpg' onload='imgSize(this, 143, 200)'/>
以上操作会产生 Undefined attribute name (onload). 警告

可以:
<body onload=' changeAllImage(143, 200)' >
……
<image src='001.jpg' />
……
</body>

posted on 2011-11-17 15:53 王添宝 阅读(152) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: