$(document).ready(function() { var maxWidth = 780; var maxHeight = 500; var zoomTimes; $("img").each(function(){ var curWidth = $(this).width(); var curHeight = $(this).height(); if(curWidth > maxWidth){ $(this).width(maxWidth); zoomTimes=curWidth/maxWidth; $(this).height(curHeight/zoomTimes); }else if(curHeight > maxHeight){ $(this).height(maxHeight); zoomTimes=curHeight/maxHeight; $(this).width(curWidth/zoomTimes); } }); });