jQuery 图片等比缩放

$(function(){

    $('.img-box img').load(function(){
        var w = $(this).width();
        var h =$(this).height();
        if (w < h){
            $(this).css('height','100%');
            var boxw =$('.img-box ').width();
            var $margin = $('.img-box ').width() - $(this).width();
            $(this).css('margin-left',$margin / 2);
        }
        else if(h < w){
            $(this).css('width','100%');
            var boxh =$('.img-box ').height();
            var $margin = $('.img-box ').height() - $(this).height();
            $(this).css('margin-top',$margin / 2);
        }
        else {
            $(this).css('width','100%').css('height','100%');
        }

    });
});
原文地址:https://www.cnblogs.com/jinsuo/p/5620127.html