﻿// 图片适应尺寸缩小的jQuery插件

(function($){
	$.fn.jNarrow = function(maxWidth, maxHeight, options){
		$(this).each(function(index,o){
			var img = document.createElement("img");
			img.onload = function(){
				if( img.width >= maxWidth ){
					img.height = img.height * maxWidth / img.width;
					img.width = maxWidth
				}
				if( maxHeight && img.height >= maxHeight ){
					img.width = img.width * maxHeight / img.height;
					img.height = maxHeight
				}
				$(o).css({
					"width": img.width +"px",
					"height": img.height +"px"
				});
				dispose()
			}
			var dispose = function(){
				img = null
			}
			img.src = $(o).attr("src");
		});
	}
})(jQuery);
