javascript - 使用 Jquery 有条件地将类添加到图像

标签 javascript jquery html css

我是 javascript 的新手,非常感谢这方面的帮助。

我想根据尺寸为特定 div 中的图像提供类。

对于肖像图像,我想给它一个类肖像,对于景观类风景图像和方形图像。我通过将宽度除以高度来完成此操作,如果该值等于 1,我将给该图像指定一类正方形,如果大于 1 个横向图像,如果小于 1 个纵向图像。

我这样做是为了在 div 中提供具有不同宽高比和不同 css 样式的图像。

我不想对所有图像执行此操作,而是对特定 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>

最佳答案

我建议您应该创建一个函数并为每个所需图像调用它:

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));
    });
});

关于javascript - 使用 Jquery 有条件地将类添加到图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52661576/

相关文章:

javascript - 每个处理程序后都会触发 Angular 变化检测吗?

html - 如何对齐另一个表格内的表格?

javascript - 找到登录的 gmail 用户电子邮件地址

javascript - 使用 jQuery 验证并从 couchdb 获取数据

javascript - Yarn 3.1,Vite 2.9,找不到包vite

jquery - 从 string/django 对象获取日期选择器默认日期

javascript - 我正在测试捕获表单上的提交按钮值

jquery - 在MVC5中为什么将@Scripts.Render放置在_Layout.cshtml中的@RenderBody标准之下

html - 如何减少打破文本的标签/跨度/p 之间的空间?

javascript - 如何将子数组包含在搜索条件中?