仅在循环中的对象下方显示一个复选框元素

时间:2018-12-31 13:05:08

标签: html css twig

我能够使用发布的代码使所有图像循环显示,但前四个隐藏在展开的元素中,但我一直努力使展开的消息仅显示一次,并且在所有元素之下。 这是单击展开消息之前的样子: enter image description here

单击以下任意一项后的外观如下: enter image description here

我尝试将<label for="hd-2">Show remaining images</label>放在循环下面,但是CSS样式停止工作。不幸的是,如果我将 label 移到循环下方(如果可能的话),我缺乏CSS知识来保持CSS样式正常工作。

### Twig ###

{% block imagelist_field %}
    <div class="imagelist columns">
        {% for image in value %}
        {% if loop.index0 is divisibleby(4) %}
            <input class="hide" id="hd-2" type="checkbox">
            <label for="hd-2">Show remaining images</label>
            <div class="section-imagelist">
        {% endif %}
        {% if value|length < 4 %}
            <div class="below4">
        {% endif %}         
        <div>
            {{ popup(image.filename, 320, 240) }}
        </div>
        {% if loop.index is divisibleby(4) or loop.last %}
            </div>
        {% endif %}
        {% if value|length < 4 %}
            </div>
        {% endif %}
    {% endfor %}
</div>
{% endblock %}

### CSS ###

.hide, .section-imagelist:nth-child(n+4)  {
  display: none;
}

.hide + label {
    margin: 0;
    padding: 0;
    color: green;
    cursor: pointer;
    display: inline-block;
    }

.hide:checked + label {
    color: red;
    border-bottom: 0;
}

.hide:checked + label ~ * {
    display: flex !important;
}

.hide + label:before {
    background-color: #1e90ff;
    color: #fff;
    content: "\002B";
    display: block;
    float: left;
    font-size: 14px; 
    font-weight: bold;
    height: 16px;
    line-height: 16px;
    margin: 3px 5px;
    text-align: center;
    width: 16px;
    -webkit-border-radius: 50%;
    -moz-border-radius: 50%;
    border-radius: 50%;
}
.hide:checked + label:before {
    content: "\2212";
}

1 个答案:

答案 0 :(得分:1)

您应该将复选框移到循环的前面,并将标签移到后面。然后,您需要调整CSS,以使其不依赖于标签和复选框彼此相邻(在标签的情况下,您实际上不需要关系选择器+)。

然后,您可以根据需要使用类.section-imagelist来显示/隐藏四个图像的组。

默认隐藏所有.section-imagelist

.section-imagelist {
  display: none;
}

默认显示第一个

.section-imagelist:first-of-type {
  display: inherit !important;
}

如果在所有.section-imagelist之前带有.hide类的选中元素,则显示

.hide:checked~.section-imagelist {
  display: inherit;
}

让我知道这是否不是您想要的。


嫩枝

{% block imagelist_field %}

    <div class="imagelist columns">

      {# The checkbox can be placed as the first child. It does not need to be adjacent to the label (the for= parameter lets it know which one to change #}
      <input class="hide" id="hd-2" type="checkbox">

      {% for image in value %}

          ...
          ...
          ...

      {% endfor %}

      {# The label should be last, so it is always after the images #}
      <label for="hd-2">Show remaining images</label>

  </div>

{% endblock %}

img {
  float: left;
  width: 25%;
}

.hide {
  display: none;
}

.hide:checked~label {
  color: red;
  border-bottom: 0;
}

.section-imagelist {
  display: none;
  width: 100%;
}

.section-imagelist:first-of-type {
    display: inline-block;
}

.hide:checked~.section-imagelist {
    display: inline-block;
}

label:before {
  background-color: #1e90ff;
  color: #fff;
  content: "\002B";
  display: block;
  float: left;
  font-size: 14px;
  font-weight: bold;
  height: 16px;
  line-height: 16px;
  margin: 3px 5px;
  text-align: center;
  width: 16px;
  -webkit-border-radius: 50%;
  -moz-border-radius: 50%;
  border-radius: 50%;
}

.hide:checked~label:before {
  content: "\2212";
  background: red;
}

label[for='hd-2'] {
  cursor: pointer;
}
<div class="imagelist columns">

  <input class="hide" id="hd-2" type="checkbox">

  <div class="section-imagelist">

    <div>
      <a>
        <img src="https://via.placeholder.com/240/09f">
      </a>
    </div>

    <div>
      <a>
        <img src="https://via.placeholder.com/240/09f">
      </a>
    </div>

    <div>
      <a>
        <img src="https://via.placeholder.com/240/09f">
      </a>
    </div>

    <div>
      <a>
        <img src="https://via.placeholder.com/240/09f">
      </a>
    </div>

  </div>


  <div class="section-imagelist below4">

    <div>
      <a>
        <img src="https://via.placeholder.com/240">
      </a>
    </div>

    <div>
      <a>
        <img src="https://via.placeholder.com/240">
      </a>
    </div>

    <div>
      <a>
        <img src="https://via.placeholder.com/240">
      </a>
    </div>


  </div>

  <label for="hd-2">Show remaining images</label>



</div>