码迷,mamicode.com
首页 > Web开发 > 详细

jquery 自动调整图片大小

时间:2014-05-26 19:40:50      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:c   class   blog   code   java   ext   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<script type="text/javascript">
   $(document).ready(function(){
               $(‘#cons img‘).each(function() {  
               var maxWidth =650; // 图片最大宽度  
               var maxHeight =650;    // 图片最大高度  
               var ratio = 0;  // 缩放比例 
                var width = $(this).width();    // 图片实际宽度  
                var height = $(this).height();  // 图片实际高度     // 检查图片是否超宽  
                if(width > maxWidth){      
                ratio = maxWidth / width;   // 计算缩放比例      
                $(this).css("width", maxWidth); // 设定实际显示宽度      
                height = height * ratio;    // 计算等比例缩放后的高度      
                 $(this).css("height", height);  // 设定等比例缩放后的高度  
                 }     // 检查图片是否超高 
                  if(height > maxHeight){      
                  ratio = maxHeight / height; // 计算缩放比例     
                   $(this).css("height", maxHeight);   // 设定实际显示高度      
                   width = width * ratio;    // 计算等比例缩放后的高度      
                   $(this).css("width", width);    // 设定等比例缩放后的高度  
                   }});
           });
  </script>

  

jquery 自动调整图片大小,布布扣,bubuko.com

jquery 自动调整图片大小

标签:c   class   blog   code   java   ext   

原文地址:http://www.cnblogs.com/cyxxzjp/p/3751835.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!