【问题标题】:responsive image layout响应式图像布局
【发布时间】:2015-12-30 17:54:14
【问题描述】:

所以我有多个具有相同类的 div,其中的图像可以从 1 到 4,具体取决于 div,我事先不知道有多少图像,因为它们来自数据库。 如何根据 div 中有多少给图像赋予不同的类。我的html

<section class="feed-section">
    <article class="feed-article">
        <div class="question-options">
            <img src="http://placehold.it/600x400">
        </div>
    </article>
    <article class="feed-article">
        <div class="question-options">
            <img src="http://placehold.it/600x400">
            <img src="http://placehold.it/600x400">
            <img src="http://placehold.it/600x400">
            <img src="http://placehold.it/600x400">
        </div>
    </article>
    <article class="feed-article">
        <div class="question-options">
            <img src="http://placehold.it/600x400">
            <img src="http://placehold.it/600x400">
            <img src="http://placehold.it/600x400">
        </div>
    </article>
</section>

我的 js

function imagesWidth() {
    var imagesConatiner = $('.question-options').each(function (index) {
        var images = $(this).children('img').length;
        switch (images) {
        case 1:
            $('.question-options img').addClass('one')
            break;
        case 2:
            $('.question-options img').addClass('two')
            break;
        case 3:
            $('.question-options img').addClass('three')
            break;
        case 4:
            $('.question-options img').addClass('four')
            break;
        default:
            console.log(images)
        }
    })
};
imagesWidth()

现在的问题是它会向所有图像添加多个类,例如它为三个添加一个的所有图像

我想用 css 来做这些

img.one {
    width:100%
}
img.two {
    width:50%
}

等等……

【问题讨论】:

  • 每个图像我仍然有 2 或 3 个类,我想要每个 div/图像一个类

标签: javascript jquery


【解决方案1】:

您是否有特定的理由为每张图片添加特定的类?您可以使用纯 css flexbox 而不使用 javascript 来实现相同的效果(确保图像在设定的宽度上均匀分布)。

.question-options {
    display: flex;
    width: 500px;
    flex-direction: row;
    padding: 10px;
    background-color: blue;
}
.question-options div {
    flex: 1 1 auto;
}
.question-options img {
    width: 100%;
}

但是,您需要将图像包装在 div 中以使其工作:

<section class="feed-section">
  <article class="feed-article">
    <div class="question-options">
        <div><img src="http://placehold.it/600x400"></div>
    </div>
  </article>
  <article class="feed-article">
    <div class="question-options">
        <div><img src="http://placehold.it/600x400"></div>
        <div><img src="http://placehold.it/600x400"></div>
        <div><img src="http://placehold.it/600x400"></div>
        <div><img src="http://placehold.it/600x400"></div>
    </div>
  </article>
  <article class="feed-article">
    <div class="question-options">
        <div><img src="http://placehold.it/600x400"></div>
        <div><img src="http://placehold.it/600x400"></div>
        <div><img src="http://placehold.it/600x400"></div>
    </div>
  </article>
</section>

小提琴:https://jsfiddle.net/grxp0gw1/

注意:Flexbox(还)不能很好地处理图像。感谢this postthis fiddle,我才得以成功。

【讨论】:

  • (更新了答案,其中包含有关使用 css 进行这项工作的更多帮助的链接)
  • 这不是 OP 想要的,OP 想要如果有多个图像,它们应该被最小化以显示容器中的所有图像
  • 类似 Flexbox 的解决方案可以完全满足 OP 的需求。它可以确保如果您有 x 个组件(图像),它们同样宽并且完全适合容器。这不就是OP想要达到的吗?
  • 是的,但是您添加的演示并没有显示 OP 想要的内容,请您更新演示
【解决方案2】:

您的代码的问题在于您使用通用选择器在图像上添加类。 $('.question-options img') 将选择元素内具有question-options 类的所有图像。您可以使用findchildren 和当前上下文($(this))或上下文选择器来选择each 内当前元素内的图像。

$('img', this).addClass('someclass');

这会将类添加到当前元素内的所有img 元素中。即.question-options 在其上进行迭代。

您的问题将通过使用上述任何一种方法来解决。

如果您正在寻找灵活优化的解决方案,

  1. 您可以使用索引作为图像数量和值作为类的数组
  2. 将索引处的类添加到所有图像

// Create array for the classes, index will be treated as number of images and value is the class to add
// Modify the array according to the maximum number of elements
var classes = ['', 'one', 'two', 'three', 'four'];

function imagesWidth() {
  var imagesConatiner = $('.question-options').each(function(index) {

    // Get the value from the array at the index i.e. no. of images and add it as class to all the images inside this container
    $('img', this).addClass(classes[$(this).children('img').length]);
  })
};
imagesWidth();
img.one {
  width: 100%
}
img.two {
  width: 50%
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<section class="feed-section">
  <article class="feed-article">
    <div class="question-options">
      <img src="http://placehold.it/600x400">
    </div>
  </article>
  <article class="feed-article">
    <div class="question-options">
      <img src="http://placehold.it/600x400">
      <img src="http://placehold.it/600x400">
      <img src="http://placehold.it/600x400">
      <img src="http://placehold.it/600x400">
    </div>
  </article>
  <article class="feed-article">
    <div class="question-options">
      <img src="http://placehold.it/600x400">
      <img src="http://placehold.it/600x400">
      <img src="http://placehold.it/600x400">
    </div>
  </article>
</section>

【讨论】:

    【解决方案3】:

    你的代码应该是这样的:

    function imagesWidth() {
        var imagesConatiner = $('.question-options').each(function (index) {
            var images = $(this).children('img').length;
            switch (images) {
            case 1:
                $(this).find('img').addClass('one')
                break;
            case 2:
                $(this).find('img').addClass('two')
                break;
            case 3:
                $(this).find('img').addClass('three')
                break;
            case 4:
                $(this).find('img').addClass('four')
                break;
            default:
                console.log(images)
            }
        })
    };
    imagesWidth()
    

    您没有将父 div 引用为“this”,因此将类添加到所有“.question-options”子“img”标签。

    【讨论】:

      【解决方案4】:

      问题在于您添加类的上下文,因为您获得了所有question-options 容器。试试这个:

      function imagesWidth() {
          var imagesConatiner = $('.question-options').each(function (index) {
              var images = $(this).children('img').length;
              var self = $(this);
      
              switch (images) {
                  case 1:
                      self.children('img').addClass('one')
                      break;
                  case 2:
                      self.children('img').addClass('two')
                      break;
                  case 3:
                      self.children('img').addClass('three')
                      break;
                  case 4:
                      self.children('img').addClass('four')
                      break;
                  default:
                      console.log(images)
              }
          })
      };
      imagesWidth()
      

      这是一个例子fiddle

      【讨论】:

        猜你喜欢
        • 2013-10-17
        • 1970-01-01
        • 2014-02-11
        • 2012-04-14
        • 1970-01-01
        • 1970-01-01
        • 2020-06-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多