JQuery 控制图片的宽高最大值

//设定最大值,也可单独设定宽或者高
 $(".content_width").autoResize({height:180,width:200})
//方法类
jQuery.fn.autoResize = function(options)
{
    var opts = {
        'width' : 500,
        'height': 250
    }
    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);
            }
        }
    });
}

posted on 2011-09-23 14:27  Cheney Hao  阅读(351)  评论(0)    收藏  举报

导航