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

时间:2021-08-21 20:28:28

 

 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         })