我正在使用
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%宽度.