1 $(document).ready(function() { 2 var maxWidth = 780; 3 var maxHeight = 500; 4 var zoomTimes; 5 $("img").each(function(){ 6 var curWidth = $(this).width(); 7 var curHeight = $(this).height(); 8 if(curWidth > maxWidth){ 9 $(this).width(maxWidth); 10 zoomTimes=curWidth/maxWidth; 11 $(this).height(curHeight/zoomTimes); 12 }else if(curHeight > maxHeight){ 13 $(this).height(maxHeight); 14 zoomTimes=curHeight/maxHeight; 15 $(this).width(curWidth/zoomTimes); 16 } 17 }); 18 });