图片(img标签)大小自适应

 

 1 $(function(){
 2              var myimg,oldwidth,oldheight;
 3                var maxwidth=249;
 4                var maxheight=187;
 5                var imgs = document.getElementsByTagName('img'); 
 6              
 7                for(i=0;i<imgs.length;i++){
 8                  myimg = imgs[i];
 9              
10                  if(myimg.width > myimg.height)
11                  {
12                      if(myimg.width > maxwidth)
13                      {
14                         oldwidth = myimg.width;
15                         myimg.height = myimg.height * (maxwidth/oldwidth);
16                         myimg.width = maxwidth;
17                      }
18                  }else{
19                      if(myimg.height > maxheight)
20                      {
21                         oldheight = myimg.height;
22                         myimg.width = myimg.width * (maxheight/oldheight);
23                         myimg.height = maxheight;
24                      }
25                  }
26                }
27         })

 

posted @ 2015-01-07 17:19  礼拜天001  阅读(5405)  评论(0编辑  收藏  举报