I'm developing a shopfront in Shopify and test whether a product image in a Flexslider is landscape or portrait:
<script type="text/javascript">
$(document).ready(function (){
$('.flexslider .slides li img').each(function() {
if ($(this).width() > $(this).height()) {
$(this).addClass('slide-landscape');
$('.flex-control-thumbs li img').addClass('thumb-landscape');
} else if ($(this).height() > $(this).width()) {
$(this).addClass('slide-portrait');
$('.flex-control-thumbs li img').addClass('thumb-portrait');
}
});
});
</script>
For some reason, the images all get the 'landscape' classes, and never the portrait. Not sure what I'm doing wrong. Any advice would be great.
Your basic if/else works for me to write classes to your main images. See fiddle: jsfiddle.net/g28ugzdp
However the 2nd statement in each condition
$('.flex-control-thumbs li img').addClass(...);actually adds the class to ALL images that are children of list items within theflex-control-thumbsclass. See fiddle: jsfiddle.net/g28ugzdp/1/You can solve for that in 1 of 2 ways.
$('.flex-control-thumbs li img')object in the same way that you do with$('.flexslider .slides li img').forloop and the array properties of your javascript objects to select each image sequentially and modify the matching thumbnail with the same conditional logic. See code below: (also on this fiddle)Also you may want to set your
ifcondition to be>=or change yourelse ifto be justelseso that you can catch any cases where the image is perfectly square.