图像等比例缩放

Js代码  
  1. $(window).bind("load"function() {  
  2.       
  3. // IMAGE RESIZE  
  4.     $('#product_cat_list img').each(function() {  
  5.         var maxWidth = 120;  
  6.         var maxHeight = 120;  
  7.         var ratio = 0;  
  8.         var width = $(this).width();  
  9.         var height = $(this).height();  
  10.         if(width > maxWidth){  
  11.             ratio = maxWidth / width;  
  12.             $(this).css("width", maxWidth);  
  13.             $(this).css("height", height * ratio);  
  14.             height = height * ratio;  
  15.         }  
  16.         var width = $(this).width();  
  17.         var height = $(this).height();  
  18.         if(height > maxHeight){  
  19.             ratio = maxHeight / height;  
  20.             $(this).css("height", maxHeight);  
  21.             $(this).css("width", width * ratio);  
  22.             width = width * ratio;  
  23.         }  
  24.     });  
  25.       
  26. //$("#contentpage img").show();  
  27.       
  28. // IMAGE RESIZE  
  29. });  
  30. 转自:http://www.iteye.com/news/28047

你可能感兴趣的:(缩放)