微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用带有复选框的纯 css 手风琴,您可以打开和关闭所有打开的标签页吗?

如何解决使用带有复选框的纯 css 手风琴,您可以打开和关闭所有打开的标签页吗?

代码来自这里 --> https://codepen.io/raubaca/pen/PZzpVe

我已经查看了类似问题的其他答案。实际上没有什么能充分回答这个问题。或者它们是关于稍微收敛的问题,或者使用 jquery 等,或者使用不同格式的 css。

当使用checkBox方法制作一个可以同时打开多个标签的纯css手风琴时,是否可以一次关闭所有标签?还要同时打开所有标签?我假设我可以执行类似 input:unchecked ~ .tab-content {...} 的操作,但是 css 规范中没有 input:unchecked

@charset "UTF-8";
body {
  color: #2c3e50;
  background: #ecf0f1;
  padding: 0 1em 1em;
}

h1 {
  margin: 0;
  line-height: 2;
  text-align: center;
}

h2 {
  margin: 0 0 0.5em;
  font-weight: normal;
}

input {
  position: absolute;
  opacity: 0;
  z-index: -1;
}

.row {
  display: flex;
}
.row .col {
  flex: 1;
}
.row .col:last-child {
  margin-left: 1em;
}

/* Accordion styles */
.tabs {
  border-radius: 8px;
  overflow: hidden;
  Box-shadow: 0 4px 4px -2px rgba(0,0.5);
}

.tab {
  width: 100%;
  color: white;
  overflow: hidden;
}
.tab-label {
  display: flex;
  justify-content: space-between;
  padding: 1em;
  background: #2c3e50;
  font-weight: bold;
  cursor: pointer;
  /* Icon */
}
.tab-label:hover {
  background: #1a252f;
}
.tab-label::after {
  content: "❯";
  width: 1em;
  height: 1em;
  text-align: center;
  transition: all 0.35s;
}
.tab-content {
  max-height: 0;
  padding: 0 1em;
  color: #2c3e50;
  background: white;
  transition: all 0.35s;
}
.tab-close,.tab-open {
  display: flex;
  justify-content: flex-end;
  padding: 1em;
  font-size: 0.75em;
  background: #2c3e50;
  cursor: pointer;
}
.tab-close:hover {
  background: #1a252f;
}

input:checked + .tab-label {
  background: #1a252f;
}
input:checked + .tab-label::after {
  transform: rotate(90deg);
}
input:checked ~ .tab-content {
  max-height: 100vh;
  padding: 1em;
}
        <!-- <h1>Pure CSS Accordion <sup>2.0</sup></h1> -->
        <div class="row">
          <div class="col">
            <h2>Open <b>multiple</b></h2>
            <div class="tabs">
                <div class="tab">
                    <input type="checkBox" id="chck0" name="chk">
                    <label for="chck0" class="tab-open">Open All &times;</label>
                  </div>
              <div class="tab">
                <input type="checkBox" id="chck1"  name="chk">
                <label class="tab-label" for="chck1">Item 1</label>
                <div class="tab-content">
                  Lorem ipsum dolor sit amet consectetur,adipisicing elit. Ipsum,reiciendis!
                </div>
              </div>
              <div class="tab">
                <input type="checkBox" id="chck2"  name="chk">
                <label class="tab-label" for="chck2">Item 2</label>
                <div class="tab-content">
                  Lorem ipsum dolor sit amet consectetur adipisicing elit. A,in!
                </div>
              </div>
              <div class="tab">
                <input type="checkBox" id="chck3" name="chk">
                <label for="chck3" class="tab-close">Close All &times;</label>
              </div>
            </div>
          </div>

解决方法

仅使用 css 可以做的最好的事情就是全部打开 - 您将无法关闭所有内容(只需将它们恢复到原始状态)。首先,您需要将切换复选框移到选项卡外,使其与手风琴选项卡处于同一级别,然后您可以使用以下代码段末尾的三种样式

如果需要全部关闭,则必须使用 js 取消选中已选中以打开它们的复选框 - css 不能这样做

@charset "UTF-8";
body {
  color: #2c3e50;
  background: #ecf0f1;
  padding: 0 1em 1em;
}

h1 {
  margin: 0;
  line-height: 2;
  text-align: center;
}

h2 {
  margin: 0 0 0.5em;
  font-weight: normal;
}

input {
  position: absolute;
  opacity: 0;
  z-index: -1;
}

.row {
  display: flex;
}

.row .col {
  flex: 1;
}

.row .col:last-child {
  margin-left: 1em;
}


/* Accordion styles */

.tabs {
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 4px 4px -2px rgba(0,0.5);
}

.tab {
  width: 100%;
  color: white;
  overflow: hidden;
}

.tab-label {
  display: flex;
  justify-content: space-between;
  padding: 1em;
  background: #2c3e50;
  font-weight: bold;
  cursor: pointer;
  /* Icon */
}

.tab-label:hover {
  background: #1a252f;
}

.tab-label::after {
  content: "❯";
  width: 1em;
  height: 1em;
  text-align: center;
  transition: all 0.35s;
}

.tab-content {
  max-height: 0;
  padding: 0 1em;
  color: #2c3e50;
  background: white;
  transition: all 0.35s;
}

.tab-close,.tab-open {
  display: flex;
  justify-content: flex-end;
  padding: 1em;
  font-size: 0.75em;
  background: #2c3e50;
  cursor: pointer;
}

.tab-close:hover {
  background: #1a252f;
}

input:checked+.tab-label {
  background: #1a252f;
}

input:checked+.tab-label::after {
  transform: rotate(90deg);
}

input:checked~.tab-content {
  max-height: 100vh;
  padding: 1em;
}

#chck0+label:before {
  content: 'Open All';
  color: #ffffff;
}

#chck0:checked+label:before {
  content: 'Return to previous state';
}

#chck0:checked~.tab .tab-content {
  max-height: 100vh;
  padding: 1em;
}
<!-- <h1>Pure CSS Accordion <sup>2.0</sup></h1> -->
<div class="row">
  <div class="col">
    <h2>Open <b>multiple</b></h2>
    <div class="tabs">
      <input type="checkbox" id="chck0" name="chk">
      <label for="chck0" class="tab-open"> &times;</label>
      <div class="tab">
        <input type="checkbox" id="chck1" name="chk">
        <label class="tab-label" for="chck1">Item 1</label>
        <div class="tab-content">
          Lorem ipsum dolor sit amet consectetur,adipisicing elit. Ipsum,reiciendis!
        </div>
      </div>
      <div class="tab">
        <input type="checkbox" id="chck2" name="chk">
        <label class="tab-label" for="chck2">Item 2</label>
        <div class="tab-content">
          Lorem ipsum dolor sit amet consectetur adipisicing elit. A,in!
        </div>
      </div>
    </div>
  </div>

实际上,您可以将其扩展为使用单选按钮作为切换按钮,然后您也可以关闭所有按钮:

@charset "UTF-8";
body {
  color: #2c3e50;
  background: #ecf0f1;
  padding: 0 1em 1em;
}

h1 {
  margin: 0;
  line-height: 2;
  text-align: center;
}

h2 {
  margin: 0 0 0.5em;
  font-weight: normal;
}

input {
  position: absolute;
  opacity: 0;
  z-index: -1;
}

.row {
  display: flex;
}

.row .col {
  flex: 1;
}

.row .col:last-child {
  margin-left: 1em;
}


/* Accordion styles */

.tabs {
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 4px 4px -2px rgba(0,.tab-open {
  display: flex;
  justify-content: flex-end;
  padding: 1em;
  font-size: 0.75em;
  background: #2c3e50;
  cursor: pointer;
}

.tab-close:hover {
  background: #1a252f;
}

input:checked+.tab-label {
  background: #1a252f;
}

input:checked+.tab-label::after {
  transform: rotate(90deg);
}

input:checked~.tab-content {
  max-height: 100vh;
  padding: 1em;
}

.toggle-label {
  color: #000000;
}

#open-all:checked~.tab .tab-content {
  max-height: 100vh;
  padding: 1em;
}

#open-all:checked~#open-all-label,#close-all:checked~#close-all-label,#reset-all:checked~#reset-all-label {
  display: none;
}

#close-all:checked~.tab .tab-content {
  max-height: 0;
  padding: 0;
}
<!-- <h1>Pure CSS Accordion <sup>2.0</sup></h1> -->
<div class="row">
  <div class="col">
    <h2>Open <b>multiple</b></h2>
    <div class="tabs">
      <input type="radio" id="reset-all" name="toggle" checked>
      <input type="radio" id="open-all" name="toggle">
      <input type="radio" id="close-all" name="toggle">
      <label for="reset-all" class="toggle-label" id="reset-all-label">Reset &times;</label>
      <label for="open-all" class="toggle-label" id="open-all-label">Open All &times;</label>
      <label for="close-all" class="toggle-label" id="close-all-label">Close All &times;</label>
      <div class="tab">
        <input type="checkbox" id="chck1" name="chk">
        <label class="tab-label" for="chck1">Item 1</label>
        <div class="tab-content">
          Lorem ipsum dolor sit amet consectetur,in!
        </div>
      </div>
    </div>
  </div>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。