JS限定图象大小

[ 2006-05-12 18:20:01 | Author: zhenhua ]
Font Size: Large | Medium | Small
全局,即当前页面中的所有图片:
function loadImgs()
{
  var imgs = document.images;
  var max_width = 500;
  for (var i=0; i<imgs.length; i++)
  {
    var w = imgs[i].width;
    var h = imgs[i].height;
    if (w > max_width)
    {
      imgs[i].width = max_width;
      imgs[i].height = h*max_width/w;
    }
  }
}

window.onload = loadImgs;

单个图片,由图片唯一ID区分
function loadImgs()
{
var imgs = document.getElementById("imgId");
  var max_width = 500;
  var w = imgs.width;
  var h = imgs.height;
  if (w > max_width)
  {
    imgs.width = max_width;
    imgs.height = h*max_width/w;
  }
}

window.onload = loadImgs;

多个图片,一类图片数组,即所有ID名都为 imgId的图片
function loadImgs()
{

var imgs = document.getElementsByName("imgId");
 var max_width = 120;
 for (i=0;i<imgs.length;i++){
   var w = imgs[i].width;
   var h = imgs[i].height;
   if (w > max_width)
   {
    imgs[i].width = max_width;
    imgs[i].height = h*max_width/w;
   }
  }
}

window.onload = loadImgs;

Comments Feed Comments Feed: http://www.zhenhua.org/feed.asp?q=comment&id=313
UTF-8 Encoding Trackback URL: http://www.zhenhua.org/trackback.asp?id=313

There is no comment on this article.

If you feel this site you find this information helpful, please click on the donation, which is voluntary,Thank you.
Post Comment
Smilies
[arrow] [biggrin] [confused] [cool]
[cry] [eek] [evil] [exclaim]
[frown] [idea] [lol] [mad]
[mrgreen] [neutral] [question] [razz]
[redface] [rolleyes] [sad] [smile]
[surprised] [twisted] [wink]
Enable UBB Codes
Auto Convert URL
Show Smilies
Hidden Comment
Username:   Password:   Register Now?
Security Code * Please Enter the Security Code