【问题标题】:Conditionally add class to image using Jquery使用 Jquery 有条件地向图像添加类
【发布时间】:2019-03-10 17:20:45
【问题描述】:

我是 javascript 的新手,非常感谢您的帮助。

我想根据尺寸对特定 div 中的图像进行分类。

对于纵向图像,我想给它一个肖像类,对于横向类风景图像和方形图像。我通过将宽度除以高度来做到这一点,如果值等于 1,我给这个图像一个正方形类,如果大于 1 个横向,如果小于 1 个纵向。

我这样做是为了给具有不同纵横比不同 css 样式的 div 中的图像。

我不想对所有图像执行此操作,而是对特定 div 中的图像执行此操作。在我的 div 类“detailsection”和 div 类“imagesection”的示例中

我无法让代码正常工作。我很难指定特定的 div 和图像。

感谢所有帮助。

$(window).on('load', function() {
  $('.imagesection > img').each(function() {
    if ($(this).width() / $(this).height() > 1) {
      $(this).addClass('landscape');
    } else if ($(this).width() / $(this).height() >= 1) {
      $(this).addClass('square');
    } else {
      $(this).addClass('portrait');
    }
  });
});

$(window).on('load', function() {
  $('.detailsection > img').each(function() {
    if ($(this).width() / $(this).height() > 1) {
      $(this).addClass('landscape');
    } else if ($(this).width() / $(this).height() >= 1) {
      $(this).addClass('square');
    } else {
      $(this).addClass('portrait');
    }
  });
});
.imagesection {
  max-width: 1000px;
  margin: 0 auto;
}

.imagesection img {
  overflow: hidden;
}

.imagesection img.landscape {
  max-width: 1000px;
  width: 70%;
  height: auto;
  margin: 5% 15%;
}

.imagesection img.portrait {
  width: 40%;
  margin: 0 auto;
  float: left;
}

.detailsection {
  max-width: 1000px;
  margin: 0 auto;
}

.detailsection img {
  overflow: hidden;
}

.detailsection img.landscape {
  max-width: 1000px;
  width: 70%;
  height: auto;
  margin: 5% 15%;
}

.detailsection img.portrait {
  width: 40%;
  margin: 0 auto;
  float: left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<div class="detailsection">
  <!--Detail Title Image-->
  <div class="titlepg">
    <img src="https://via.placeholder.com/1000x1000" alt="" class="titlepg">
  </div>
  <!--Detail Title Image-->

  <!--Detail Spec Image-->
  <div class="specpg">
    <img src="https://via.placeholder.com/1000x1000" alt="" class="specpg">
  </div>
  <!--Detail Spec Image-->

  <!--Detail Option Image-->
  <div class="optionpg">
    <img src="https://via.placeholder.com/1000x1000" alt="" class="optionpg">
  </div>
  <!--Detail Option Image-->

  <!--Detail Detail Image pg-->
  <div class="detailimagepg">
    <img src="https://via.placeholder.com/1000x500" />
    <img src="https://via.placeholder.com/500x1000" />
    <img src="https://via.placeholder.com/500x1000" />
  </div>
  <!--Detail Detail Image pg-->

</div>

<div class="imagesection">
  <img src="https://via.placeholder.com/1000x500" />
  <img src="https://via.placeholder.com/500x1000" />
  <img src="https://via.placeholder.com/500x1000" />
</div>

【问题讨论】:

  • 在您的每个 else if 语句之后首先缺少一个 }
  • 另一个问题是.detailsection &gt; img 不会选择您的detailsection 类中的任何图像,因为它们不是直系后代。
  • 这就是我的做法:jsfiddle.net/websiter/423kj6ur/1。选择器是$('img', '.imagesection, .detailsection'),类分配在addClass() 内完成,使用三元检查,因为它们非常简单。当然,每个&lt;div&gt; 不需要两个代码块。见DRY

标签: javascript jquery html css


【解决方案1】:

从您的选择器中删除 >。

.imagesection > img 

变成

.imagesection img

【讨论】:

    【解决方案2】:

    我建议您应该创建一个函数并将其调用为所需图像的 foreach:

    function applyImageClass(image) {
       var h = (image) ? image.height() : 0;
       if (h > 0) {
           var ratio = image.width() / h;
           if (ratio === 1) {
               image.addClass('square');
           } else if (ratio > 1) {
               image.addClass('landscape');
           } else if (ratio < 1) {
               image.addClass('portrait');
           }
       }      
    }
    
    $(window).on('load', function() {
        // Find all images contained in any element that has any of the tow classes
        $('.imagesection, .detailsection').find('img').each(function() {
            // Call the function for each one of the matched elements
            applyImageClass($(this));
        });
    });
    

    【讨论】:

    • $('.imagesection, .detailsection').find('img') => $('img','.imagesection,.detailsection')
    【解决方案3】:

    在代码中正确使用 } 关闭 else if 语句。

    然后从选择器中删除 >。

    '.detailsection > img'
    

    改为

    '.detailsection img'
    

    为了获得清晰的代码,我推荐@kapantzak 回答。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-16
      • 2014-04-10
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多