半年前写的代码,现在看看也不算太差就放上来了
$(document).ready(function(){
$('div').autoResize({height:50}); //用法
});
jQuery.fn.autoResize = function(options)
{
var opts = {
'width' : 400,
'height': 300
}
var opt = $.extend(true, {},opts,options || {});
width = opt.width;
height = opt.height;
$('img',this).each(function(){
var image = new Image();
image.src = $(this).attr('src');
//开始检查图片
if(image.width > 0 && image.height > 0 ){
var image_rate = 1;
if( (width / image.width) < (height / image.height)){
image_rate = width / image.width ;
}else{
image_rate = height / image.height ;
}
if ( image_rate <= 1){
$(this).width(image.width * image_rate);
$(this).height(image.height * image_rate);
}
}
});
}
用法也写在上面了。可以直接使用。。。