简体   繁体   English

Shopify中的横向或纵向图像的JQuery测试

[英]JQuery test for landscape or portrait image in Shopify

I'm developing a shopfront in Shopify and test whether a product image in a Flexslider is landscape or portrait: 我正在Shopify中开发店面,并测试Flexslider中的产品图片是横向还是纵向:

    <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. 您的基本if / else对我有用,可以为您的主图像编写类。 See fiddle: jsfiddle.net/g28ugzdp 参见小提琴: jsfiddle.net/g28ugzdp

However the 2nd statement in each condition $('.flex-control-thumbs li img').addClass( ... ); 但是,每个条件中的第二条语句$('.flex-control-thumbs li img').addClass( ... ); actually adds the class to ALL images that are children of list items within the flex-control-thumbs class. 实际上将该类添加到flex-control-thumbs类中属于列表项的子项的所有图像中。 See fiddle: jsfiddle.net/g28ugzdp/1/ 参见提琴: jsfiddle.net/g28ugzdp/1/

You can solve for that in 1 of 2 ways. 您可以通过2种方式之一解决该问题。

  1. If your thumbnail images are displayed at their correct aspect ratios, you can simply run the main loop over the $('.flex-control-thumbs li img') object in the same way that you do with $('.flexslider .slides li img') . 如果缩略图以正确的纵横比显示,则可以像处理$('.flexslider .slides li img')一样,在$('.flex-control-thumbs li img')对象上运行主循环$('.flexslider .slides li img')
  2. If your thumbnails need to have their classes match the main image, but don't render on the page in the correct aspect ratio, then I would use a for loop and the array properties of your javascript objects to select each image sequentially and modify the matching thumbnail with the same conditional logic. 如果您的缩略图需要使它们的类与主图像匹配,但是没有以正确的宽高比显示在页面上,则可以使用for循环和javascript对象的array属性依次选择每个图像并修改具有相同条件逻辑的匹配缩略图。 See code below: (also on this fiddle ) 参见下面的代码:(也在这个小提琴上

 $(document).ready(function(){ var images = $('.flexslider .slides li img'), thumbs = $('.flex-control-thumbs li img'); for ( var i = 0; i < images.length; i++ ) { if (images[i].width > images[i].height) { $(images[i]).addClass('slide-landscape'); $(thumbs[i]).addClass('thumb-landscape'); } else { $(images[i]).addClass('slide-portrait'); $(thumbs[i]).addClass('thumb-portrait'); } }; }); 
 .slide-landscape {border-top:8px solid orange;} .slide-portrait {border-left:8px solid green;} .thumb-landscape {border-top:4px solid red;} .thumb-portrait {border-left:4px solid blue;} li {display:inline-block;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="flexslider"> <article class="flex-control-thumbs"> <ul> <li><img src="http://placekitten.com/g/20/40" /></li> <li><img src="http://placekitten.com/g/40/20" /></li> <li><img src="http://placekitten.com/g/20/40" /></li> <li><img src="http://placekitten.com/g/40/20" /></li> </ul> </article> <article class="slides"> <ul> <li><img src="http://placekitten.com/g/200/300" /></li> <li><img src="http://placekitten.com/g/300/200" /></li> <li><img src="http://placekitten.com/g/200/300" /></li> <li><img src="http://placekitten.com/g/300/200" /></li> </ul> </article> </div> 

Also you may want to set your if condition to be >= or change your else if to be just else so that you can catch any cases where the image is perfectly square. 另外,您可能希望将if条件设置为>=或将else if设置为else以便可以捕获图像完全为正方形的任何情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM