gpt4 book ai didi

javascript - 如何使 CSS Accordion 在从一个部分转到另一个部分时自动关闭打开的内容

转载 作者:搜寻专家 更新时间:2023-10-31 08:43:26 24 4
gpt4 key购买 nike

我有一个 css Accordion ,我需要在单击每个标题时自动关闭它。这样一次只会打开一个部分。现在它们都保持开放状态。

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

关于javascript - 如何使 CSS Accordion 在从一个部分转到另一个部分时自动关闭打开的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32685037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com