关于图片加载后遍历 和 ajax遍历

需求: 根据图片的竖图、横图(比较长宽),用不同的样式。横图:width:100%; 竖图:height: 100%。需要兼容ie8

解决方案:

1.用js判定横图竖图,然后添加对应的css。

中途遇到一个加载判定的问题,如何判定图片已加载后去判断图片是横竖图?

//证书封面样式判定后修改
window.onload = function(){
        $('.J-certCover-img').each(function () {
            var w = $(this).width();
            var h = $(this).height();
            if(h > w){
                   $(this).css('width','auto');
                   $(this).css('height','100%');
                   $(this).css('margin','0 auto');
            }
        })
    }

在ajax中:

//证书封面样式判定后修改
        $('.J-certCover-img').on('load',function(){
            var w = $(this).width();
            var h = $(this).height();
            if(h > w){
                   $(this).css('width','auto');
                   $(this).css('height','100%');
                   $(this).css('margin','0 auto');
            }
            
        })
原文地址:https://www.cnblogs.com/monozxy/p/13453182.html