繁体   English   中英

如何在从一个部分到另一个部分时使CSS手风琴自动关闭打开内容

[英]How to make a CSS accordion auto close open content when going from section to section

我有一把css手风琴,我需要在点击每个标题时自动关闭。 这样一次只能打开一个部分。 现在他们都保持开放。

HTML

<section class="ac-container">

  <div>
    <input id="ac-1" name="accordion-1" type="checkbox" />
    <label for="ac-1"> title 1</label>
    <article class="ac-small">
      <p>some content</p>
    </article>
  </div>
  <div>
    <input id="ac-2" name="accordion-1" type="checkbox" />
    <label for="ac-2">title 2</label>
    <article class="ac-small">
      <p>some content </p>
    </article>
  </div>
  <div>
    <input id="ac-3" name="accordion-1" type="checkbox" />
    <label for="ac-3">title 3</label>
    <article class="ac-small">
      <p>some content</p>
    </article>
  </div>

</section>

CSS

.ac-container {
    margin-left: 200px;
    margin-top: 100px;
}

.ac-container h3 {
    font-size: 1.2em;
    margin-bottom: 25px;
    margin-top: 5px;
    margin-left: -70px;
    width: 500px
}

.ac-container h1 {
    font-size: 3.2em;
    margin-bottom: 10px;
    width: 370px;
    color: #eb519c; 
}

.ac-container p {
    margin-left: 40px
}

.ac-container{
    width: 450px;
    margin: 10px auto 30px auto;
    text-align: left;
    height: auto;
    background-color: #2d2d2d;
}
.ac-container label{
    font-family: 'forced_squaremedium', helvetica     , sans-serif;
    padding: 5px 60px; 
    position: relative;
    text-transform: lowercase;
    z-index: 20;
    display: block;
    height: auto;
    cursor: pointer;
    color: #fff;
    line-height: 33px;
    font-size: 1.5em;
    background: #ffffff;
}
.ac-container label:hover{
    background: #2d2d2d;
}
.ac-container input:checked + label,
.ac-container input:checked + label:hover{
    background: #2d2d2d;
    color: #5dc8d6;
    background-image: url(../img/pinkBulletTurn.png);
    background-repeat: no-repeat;
    background-position: 3.1% -.1%;
}

.ac-container label:hover:after,
.ac-container input:checked + label:hover:after{
    content: '';
    position: absolute;
    width: 24px;
    height: 24px;
    right: 413px;
    top: 7px;
    background: transparent url(../img/whiteBullet.png) no-repeat center center;    
}
.ac-container input:checked + label:hover:after{
    background-image: url(../img/blueBullet.png);
    background-position:center bottom;
}
.ac-container input{
    display: none;
    background-image: url(../img/whiteBullet.png);
}
.ac-container article{
    background: #43535b;
    margin-top: -1px;
    overflow: hidden;
    height: 0px;
    position: relative;
    z-index: 10;
    -webkit-transition: height 0.3s ease-in-out, box-shadow 0.6s linear;
    -moz-transition: height 0.3s ease-in-out, box-shadow 0.6s linear;
    -o-transition: height 0.3s ease-in-out, box-shadow 0.6s linear;
    -ms-transition: height 0.3s ease-in-out, box-shadow 0.6s linear;
    transition: height 0.3s ease-in-out, box-shadow 0.6s linear;
}
.ac-container article p{
    font-style: italic;
    color: #fff;
    line-height: 23px;
    font-size: 14px;
    padding: 20px;
}
.ac-container input:checked ~ article{
    -webkit-transition: height 0.5s ease-in-out, box-shadow 0.1s linear;
    -moz-transition: height 0.5s ease-in-out, box-shadow 0.1s linear;
    -o-transition: height 0.5s ease-in-out, box-shadow 0.1s linear;
    -ms-transition: height 0.5s ease-in-out, box-shadow 0.1s linear;
    transition: height 0.5s ease-in-out, box-shadow 0.1s linear;
}
.ac-container input:checked ~ article.ac-small{
    height: auto;
}
.ac-container input:checked ~ article.ac-medium{
    height: 180px;
}
.ac-container input:checked ~ article.ac-large{
    height: 230px;
}

.ac-container label {
    background-color: #2d2d2d;
    background-image: url(../img/pinkBullet.png);
    background-repeat: no-repeat;
    background-position: 2.5%
}

我制作了它的代码笔: http//codepen.io/anon/pen/zvBKby

谢谢!

您只需要在input元素中进行更改:

type="checkbox"

至:

type="radio"

但是你应该注意到这一点,总是会打开一个标签。

DEMO

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM