小编SGP*_*coe的帖子

在jquery中找到图像方向?

我正在使用

jQuery(document).ready(function() {
    jQuery("img").each(function() {

        // Calculate aspect ratio and store it in HTML data- attribute
        var aspectRatio = jQuery(this).width()/jQuery(this).height();
        jQuery(this).data("aspect-ratio", aspectRatio);
               // Conditional statement

        if(aspectRatio > 1) {
            // Image is landscape

            jQuery( this ).addClass( "landscape" );

        } else if (aspectRatio < 1) {
            // Image is portrait
            jQuery( this ).addClass( "portrait" );;
        } else {
            // Image is square
            jQuery( this ).addClass( "square" );;            
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

但它每次都只是回归景观.

我想做的就是有一个肖像图像类,所以我可以使用CSS使它们50%宽度并排,而景观图像是100%宽度.

javascript css wordpress jquery

1
推荐指数
1
解决办法
278
查看次数

标签 统计

css ×1

javascript ×1

jquery ×1

wordpress ×1