单击关闭手风琴选项卡
Accordion tabs closing on click
我有一些非常基本的手风琴选项卡,单击时打开,单击另一个时关闭。我希望它们也能够在您单击打开的选项卡时关闭。有谁知道如何做到这一点?
.ac-container {
// max-width: 400px;
}
.ac-container label {
// height: 30px !important;
line-height: 21px !important;
// font-size: 12px !important;
padding: 5px 20px;
text-transform: uppercase;
font-weight: 100;
position: relative;
z-index: 20;
display: block;
height: 30px;
cursor: pointer;
color: white;
line-height: 33px;
font-size: 19px;
background: $dark-blue;
border-bottom: 2px solid $light-blue;
height: auto;
line-height: 20px;
}
.ac-container span {
position:absolute;
bottom:5px;
right:8px;
}
.ac-container {
width: 100%;
// margin: 10px auto 30px auto;
text-align: left;
}
.ac-container label:hover {
background: $light-blue;
color:$dark-blue;
}
.ac-container input:checked + label,.ac-container input:checked + label:hover {
background: $dark-blue;
color: white;
text-transform: uppercase;
font-weight: 100;
height: auto;
line-height: 21px;
font-size: 19px;
}
.ac-container label:hover:after,.ac-container input:checked + label:hover:after {
content: '';
position: absolute;
width: 24px;
height: 24px;
right: 13px;
top: 7px;
// background: transparent url(../images/arrow_down.png) no-repeat center center;
}
.ac-container input:checked + label:hover:after {
// background-image: url(../images/arrow_up.png);
}
.ac-container input {
display: none;
}
.ac-container article {
background: rgba(255,255,255,0.5);
margin-top: -1px;
overflow: hidden;
height: 0;
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: normal;
color: #777;
line-height: 23px;
font-size: 100%;
padding: 20px;
text-shadow: 1px 1px 1px rgba(255,255,255,0.8);
}
.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;
box-shadow: 0 0 0 1px rgba(155,155,155,0.3);
}
.ac-container input:checked ~ article.ac-small {
height: 140px;
}
.ac-container input:checked ~ article.ac-medium {
height: 180px;
}
.ac-container input:checked ~ article.ac-large {
height: 230px;
}
<section class="ac-container">
<h2>Breakfast</h2>
<div>
<input id="ac-1" name="accordion-1" type="radio" >
<label for="ac-1"><img src="img/breakfast/american.jpg" alt=""><span>American</span></label>
<article class="ac-small">
<p>Well, the way they make shows is, they make one show. That show's called a pilot. Then they show that show to the people who make shows, and on the strength of that one show they decide if they're going to make more shows.</p>
</article>
</div>
<div>
<input id="ac-2" name="accordion-1" type="radio">
<label for="ac-2"><img src="img/breakfast/continental.jpg" alt=""></i><span>Continental</span></label>
<article class="ac-medium">
<p>Like you, I used to think the world was this great place where everybody lived by the same standards I did, then some kid with a nail showed me I was living in his world, a world where chaos rules not order, a world where righteousness is not rewarded. That's Cesar's world, and if you're not willing to play by his rules, then you're gonna have to pay the price. </p>
</article>
</div>
<div>
<input id="ac-3" name="accordion-1" type="radio">
<label for="ac-3"><img src="img/breakfast/english.jpg" alt=""><span>English</span></label>
<article class="ac-large">
<p>You think water moves fast? You should see ice. It moves like it has a mind. Like it knows it killed the world once and got a taste for murder. After the avalanche, it took us a week to climb out. Now, I don't know exactly when we turned on each other, but I know that seven of us survived the slide... and only five made it out. Now we took an oath, that I'm breaking now. We said we'd say it was the snow that killed the other two, but it wasn't. Nature is lethal but it doesn't hold a candle to man. </p>
</article>
</div>
<div>
<input id="ac-4" name="accordion-1" type="radio">
<label for="ac-4"><img src="img/breakfast/health.jpg" alt=""><span>The Healthy Stuff</span></label>
<article class="ac-large">
<p>You see? It's curious. Ted did figure it out - time travel. And when we get back, we gonna tell everyone. How it's possible, how it's done, what the dangers are. But then why fifty years in the future when the spacecraft encounters a black hole does the computer call it an 'unknown entry event'? Why don't they know? If they don't know, that means we never told anyone. And if we never told anyone it means we never made it back. Hence we die down here. Just as a matter of deductive logic. </p>
</article>
</div>
<div>
<input id="ac-5" name="accordion-1" type="radio">
<label for="ac-5"><img src="img/breakfast/misc.jpg" alt=""><span>Miscellaneous</span></label>
<article class="ac-large">
<p>You see? It's curious. Ted did figure it out - time travel. And when we get back, we gonna tell everyone. How it's possible, how it's done, what the dangers are. But then why fifty years in the future when the spacecraft encounters a black hole does the computer call it an 'unknown entry event'? Why don't they know? If they don't know, that means we never told anyone. And if we never told anyone it means we never made it back. Hence we die down here. Just as a matter of deductive logic. </p>
</article>
</div>
</section>
您的代码无效。
您的 HTML 没有 H4 标签。
这是一个工作示例,可以按您想要的方式工作。
$(document).ready(function() {
$('.accordion-title').click(function() {
if ($(this).hasClass('active')) {
$(this).removeClass('active').next('.accordion-content').slideUp();
} else {
$('.accordion-title').removeClass('active');
$('.accordion-content').slideUp();
$(this).addClass('active').next('.accordion-content').slideDown();
}
});
});
.accordion-title {
font-size: 14px;
font-weight: bold;
border: 1px solid black;
}
.accordion-content {
border: 1px solid black;
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="accordion-title">Title</div>
<div class="accordion-content">Content</div>
<div class="accordion-title">Title 2</div>
<div class="accordion-content">Content 2</div>
<div class="accordion-title">Title 3</div>
<div class="accordion-content">Content 3</div>
使用输入类型复选框而不是单选框,并在 css 中编写样式,以便在未选中时使高度为零。
<section class="ac-container">
<h2>Breakfast</h2>
<div>
<input id="ac-1" name="accordion-1" type="checkbox" >
<label for="ac-1"><img src="img/breakfast/american.jpg" alt=""> <span>American</span></label>
<article class="ac-small">
<p>Well, the way they make shows is, they make one show. That show's called a pilot. Then they show that show to the people who make shows, and on the strength of that one show they decide if they're going to make more shows.</p>
</article>
</div>
<div>
<input id="ac-2" name="accordion-1" type="checkbox">
<label for="ac-2"><img src="img/breakfast/continental.jpg" alt=""></i><span>Continental</span></label>
<article class="ac-medium">
<p>Like you, I used to think the world was this great place where everybody lived by the same standards I did, then some kid with a nail showed me I was living in his world, a world where chaos rules not order, a world where righteousness is not rewarded. That's Cesar's world, and if you're not willing to play by his rules, then you're gonna have to pay the price. </p>
</article>
</div>
<div>
<input id="ac-3" name="accordion-1" type="checkbox">
<label for="ac-3"><img src="img/breakfast/english.jpg" alt=""><span>English</span></label>
<article class="ac-large">
<p>You think water moves fast? You should see ice. It moves like it has a mind. Like it knows it killed the world once and got a taste for murder. After the avalanche, it took us a week to climb out. Now, I don't know exactly when we turned on each other, but I know that seven of us survived the slide... and only five made it out. Now we took an oath, that I'm breaking now. We said we'd say it was the snow that killed the other two, but it wasn't. Nature is lethal but it doesn't hold a candle to man. </p>
</article>
</div>
<div>
<input id="ac-4" name="accordion-1" type="checkbox">
<label for="ac-4"><img src="img/breakfast/health.jpg" alt=""><span>The Healthy Stuff</span></label>
<article class="ac-large">
<p>You see? It's curious. Ted did figure it out - time travel. And when we get back, we gonna tell everyone. How it's possible, how it's done, what the dangers are. But then why fifty years in the future when the spacecraft encounters a black hole does the computer call it an 'unknown entry event'? Why don't they know? If they don't know, that means we never told anyone. And if we never told anyone it means we never made it back. Hence we die down here. Just as a matter of deductive logic. </p>
</article>
</div>
<div>
<input id="ac-5" name="accordion-1" type="checkbox">
<label for="ac-5"><img src="img/breakfast/misc.jpg" alt=""><span>Miscellaneous</span></label>
<article class="ac-large">
<p>You see? It's curious. Ted did figure it out - time travel. And when we get back, we gonna tell everyone. How it's possible, how it's done, what the dangers are. But then why fifty years in the future when the spacecraft encounters a black hole does the computer call it an 'unknown entry event'? Why don't they know? If they don't know, that means we never told anyone. And if we never told anyone it means we never made it back. Hence we die down here. Just as a matter of deductive logic. </p>
</article>
</div>
</section>
我有一些非常基本的手风琴选项卡,单击时打开,单击另一个时关闭。我希望它们也能够在您单击打开的选项卡时关闭。有谁知道如何做到这一点?
.ac-container {
// max-width: 400px;
}
.ac-container label {
// height: 30px !important;
line-height: 21px !important;
// font-size: 12px !important;
padding: 5px 20px;
text-transform: uppercase;
font-weight: 100;
position: relative;
z-index: 20;
display: block;
height: 30px;
cursor: pointer;
color: white;
line-height: 33px;
font-size: 19px;
background: $dark-blue;
border-bottom: 2px solid $light-blue;
height: auto;
line-height: 20px;
}
.ac-container span {
position:absolute;
bottom:5px;
right:8px;
}
.ac-container {
width: 100%;
// margin: 10px auto 30px auto;
text-align: left;
}
.ac-container label:hover {
background: $light-blue;
color:$dark-blue;
}
.ac-container input:checked + label,.ac-container input:checked + label:hover {
background: $dark-blue;
color: white;
text-transform: uppercase;
font-weight: 100;
height: auto;
line-height: 21px;
font-size: 19px;
}
.ac-container label:hover:after,.ac-container input:checked + label:hover:after {
content: '';
position: absolute;
width: 24px;
height: 24px;
right: 13px;
top: 7px;
// background: transparent url(../images/arrow_down.png) no-repeat center center;
}
.ac-container input:checked + label:hover:after {
// background-image: url(../images/arrow_up.png);
}
.ac-container input {
display: none;
}
.ac-container article {
background: rgba(255,255,255,0.5);
margin-top: -1px;
overflow: hidden;
height: 0;
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: normal;
color: #777;
line-height: 23px;
font-size: 100%;
padding: 20px;
text-shadow: 1px 1px 1px rgba(255,255,255,0.8);
}
.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;
box-shadow: 0 0 0 1px rgba(155,155,155,0.3);
}
.ac-container input:checked ~ article.ac-small {
height: 140px;
}
.ac-container input:checked ~ article.ac-medium {
height: 180px;
}
.ac-container input:checked ~ article.ac-large {
height: 230px;
}
<section class="ac-container">
<h2>Breakfast</h2>
<div>
<input id="ac-1" name="accordion-1" type="radio" >
<label for="ac-1"><img src="img/breakfast/american.jpg" alt=""><span>American</span></label>
<article class="ac-small">
<p>Well, the way they make shows is, they make one show. That show's called a pilot. Then they show that show to the people who make shows, and on the strength of that one show they decide if they're going to make more shows.</p>
</article>
</div>
<div>
<input id="ac-2" name="accordion-1" type="radio">
<label for="ac-2"><img src="img/breakfast/continental.jpg" alt=""></i><span>Continental</span></label>
<article class="ac-medium">
<p>Like you, I used to think the world was this great place where everybody lived by the same standards I did, then some kid with a nail showed me I was living in his world, a world where chaos rules not order, a world where righteousness is not rewarded. That's Cesar's world, and if you're not willing to play by his rules, then you're gonna have to pay the price. </p>
</article>
</div>
<div>
<input id="ac-3" name="accordion-1" type="radio">
<label for="ac-3"><img src="img/breakfast/english.jpg" alt=""><span>English</span></label>
<article class="ac-large">
<p>You think water moves fast? You should see ice. It moves like it has a mind. Like it knows it killed the world once and got a taste for murder. After the avalanche, it took us a week to climb out. Now, I don't know exactly when we turned on each other, but I know that seven of us survived the slide... and only five made it out. Now we took an oath, that I'm breaking now. We said we'd say it was the snow that killed the other two, but it wasn't. Nature is lethal but it doesn't hold a candle to man. </p>
</article>
</div>
<div>
<input id="ac-4" name="accordion-1" type="radio">
<label for="ac-4"><img src="img/breakfast/health.jpg" alt=""><span>The Healthy Stuff</span></label>
<article class="ac-large">
<p>You see? It's curious. Ted did figure it out - time travel. And when we get back, we gonna tell everyone. How it's possible, how it's done, what the dangers are. But then why fifty years in the future when the spacecraft encounters a black hole does the computer call it an 'unknown entry event'? Why don't they know? If they don't know, that means we never told anyone. And if we never told anyone it means we never made it back. Hence we die down here. Just as a matter of deductive logic. </p>
</article>
</div>
<div>
<input id="ac-5" name="accordion-1" type="radio">
<label for="ac-5"><img src="img/breakfast/misc.jpg" alt=""><span>Miscellaneous</span></label>
<article class="ac-large">
<p>You see? It's curious. Ted did figure it out - time travel. And when we get back, we gonna tell everyone. How it's possible, how it's done, what the dangers are. But then why fifty years in the future when the spacecraft encounters a black hole does the computer call it an 'unknown entry event'? Why don't they know? If they don't know, that means we never told anyone. And if we never told anyone it means we never made it back. Hence we die down here. Just as a matter of deductive logic. </p>
</article>
</div>
</section>
您的代码无效。
您的 HTML 没有 H4 标签。
这是一个工作示例,可以按您想要的方式工作。
$(document).ready(function() {
$('.accordion-title').click(function() {
if ($(this).hasClass('active')) {
$(this).removeClass('active').next('.accordion-content').slideUp();
} else {
$('.accordion-title').removeClass('active');
$('.accordion-content').slideUp();
$(this).addClass('active').next('.accordion-content').slideDown();
}
});
});
.accordion-title {
font-size: 14px;
font-weight: bold;
border: 1px solid black;
}
.accordion-content {
border: 1px solid black;
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="accordion-title">Title</div>
<div class="accordion-content">Content</div>
<div class="accordion-title">Title 2</div>
<div class="accordion-content">Content 2</div>
<div class="accordion-title">Title 3</div>
<div class="accordion-content">Content 3</div>
使用输入类型复选框而不是单选框,并在 css 中编写样式,以便在未选中时使高度为零。
<section class="ac-container">
<h2>Breakfast</h2>
<div>
<input id="ac-1" name="accordion-1" type="checkbox" >
<label for="ac-1"><img src="img/breakfast/american.jpg" alt=""> <span>American</span></label>
<article class="ac-small">
<p>Well, the way they make shows is, they make one show. That show's called a pilot. Then they show that show to the people who make shows, and on the strength of that one show they decide if they're going to make more shows.</p>
</article>
</div>
<div>
<input id="ac-2" name="accordion-1" type="checkbox">
<label for="ac-2"><img src="img/breakfast/continental.jpg" alt=""></i><span>Continental</span></label>
<article class="ac-medium">
<p>Like you, I used to think the world was this great place where everybody lived by the same standards I did, then some kid with a nail showed me I was living in his world, a world where chaos rules not order, a world where righteousness is not rewarded. That's Cesar's world, and if you're not willing to play by his rules, then you're gonna have to pay the price. </p>
</article>
</div>
<div>
<input id="ac-3" name="accordion-1" type="checkbox">
<label for="ac-3"><img src="img/breakfast/english.jpg" alt=""><span>English</span></label>
<article class="ac-large">
<p>You think water moves fast? You should see ice. It moves like it has a mind. Like it knows it killed the world once and got a taste for murder. After the avalanche, it took us a week to climb out. Now, I don't know exactly when we turned on each other, but I know that seven of us survived the slide... and only five made it out. Now we took an oath, that I'm breaking now. We said we'd say it was the snow that killed the other two, but it wasn't. Nature is lethal but it doesn't hold a candle to man. </p>
</article>
</div>
<div>
<input id="ac-4" name="accordion-1" type="checkbox">
<label for="ac-4"><img src="img/breakfast/health.jpg" alt=""><span>The Healthy Stuff</span></label>
<article class="ac-large">
<p>You see? It's curious. Ted did figure it out - time travel. And when we get back, we gonna tell everyone. How it's possible, how it's done, what the dangers are. But then why fifty years in the future when the spacecraft encounters a black hole does the computer call it an 'unknown entry event'? Why don't they know? If they don't know, that means we never told anyone. And if we never told anyone it means we never made it back. Hence we die down here. Just as a matter of deductive logic. </p>
</article>
</div>
<div>
<input id="ac-5" name="accordion-1" type="checkbox">
<label for="ac-5"><img src="img/breakfast/misc.jpg" alt=""><span>Miscellaneous</span></label>
<article class="ac-large">
<p>You see? It's curious. Ted did figure it out - time travel. And when we get back, we gonna tell everyone. How it's possible, how it's done, what the dangers are. But then why fifty years in the future when the spacecraft encounters a black hole does the computer call it an 'unknown entry event'? Why don't they know? If they don't know, that means we never told anyone. And if we never told anyone it means we never made it back. Hence we die down here. Just as a matter of deductive logic. </p>
</article>
</div>
</section>