我创建了一个手风琴,但是当我选择尝试最小化“ Button 2”时,它将最小化“ Button 1”。但是,当我尝试最小化“按钮1”时,它会最小化自身。我在JS Fiddle上发布了代码,以供您参考,以便您可以看到实际的效果:
https://jsfiddle.net/tay7p4eL/2/
有什么主意我在做错什么,以及如何解决这个问题,以便Button 2可以将自身最小化,并且不会影响Button 1?
HTML:
<div class="wrap-collabsible">
<input checked="checked" class="toggle" id="collapsible" type="checkbox" checked> <label class="lbl-toggle" for="collapsible" tabindex="0">Button 1</label>
<div class="collapsible-content">
<div class="content-inner">
<p>Test 1</p>
</div>
</div>
</div>
按钮2
测试2
CSS:
.wrap-collabsible {
margin-bottom: 1.2rem 0;
}
input[type='checkbox'] {
display: none;
}
.lbl-toggle {
display: block;
font-weight: bold;
font-family: monospace;
font-size: 1.2rem;
text-transform: uppercase;
text-align: center;
padding: 1rem;
color: #A77B0E;
background: #FAE042;
cursor: pointer;
border-radius: 7px;
transition: all 0.25s ease-out;
}
.lbl-toggle:hover {
color: #7C5A0B;
}
.lbl-toggle::before {
content: ' ';
display: inline-block;
border-top: 5px solid transparent;
border-bottom: 5px solid transparent;
border-left: 5px solid currentColor;
vertical-align: middle;
margin-right: .7rem;
transform: translateY(-2px);
transition: transform .2s ease-out;
}
.toggle:checked + .lbl-toggle::before {
transform: rotate(90deg) translateX(-3px);
}
.collapsible-content {
max-height: 0px;
overflow: hidden;
transition: max-height .25s ease-in-out;
}
.toggle:checked + .lbl-toggle + .collapsible-content {
max-height: 350px;
}
.toggle:checked + .lbl-toggle {
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
}
.collapsible-content .content-inner {
background: rgba(250, 224, 66, .2);
border-bottom: 1px solid rgba(250, 224, 66, .45);
border-bottom-left-radius: 7px;
border-bottom-right-radius: 7px;
padding: .5rem 1rem;
}
答案 0 :(得分:1)
在html中,您已经为“按钮1”和“按钮2”输入分配了相同的ID“可折叠”。为每个输入分配唯一的ID(并更正相应的html标签)应该可以解决您的问题。
例如:
.wrap-collabsible {
margin-bottom: 1.2rem 0;
}
input[type='checkbox'] {
display: none;
}
.lbl-toggle {
display: block;
font-weight: bold;
font-family: monospace;
font-size: 1.2rem;
text-transform: uppercase;
text-align: center;
padding: 1rem;
color: #A77B0E;
background: #FAE042;
cursor: pointer;
border-radius: 7px;
transition: all 0.25s ease-out;
}
.lbl-toggle:hover {
color: #7C5A0B;
}
.lbl-toggle::before {
content: ' ';
display: inline-block;
border-top: 5px solid transparent;
border-bottom: 5px solid transparent;
border-left: 5px solid currentColor;
vertical-align: middle;
margin-right: .7rem;
transform: translateY(-2px);
transition: transform .2s ease-out;
}
.toggle:checked + .lbl-toggle::before {
transform: rotate(90deg) translateX(-3px);
}
.collapsible-content {
max-height: 0px;
overflow: hidden;
transition: max-height .25s ease-in-out;
}
.toggle:checked + .lbl-toggle + .collapsible-content {
max-height: 350px;
}
.toggle:checked + .lbl-toggle {
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
}
.collapsible-content .content-inner {
background: rgba(250, 224, 66, .2);
border-bottom: 1px solid rgba(250, 224, 66, .45);
border-bottom-left-radius: 7px;
border-bottom-right-radius: 7px;
padding: .5rem 1rem;
}
<div class="wrap-collabsible">
<input checked="checked" class="toggle" id="collapsible1" type="checkbox" checked>
<label class="lbl-toggle" for="collapsible1" tabindex="0">Button 1</label>
<div class="collapsible-content">
<div class="content-inner">
<p>Test 1</p>
</div>
</div>
</div>
<div class="wrap-collabsible">
<input checked="checked" class="toggle" id="collapsible2" type="checkbox" checked>
<label class="lbl-toggle" for="collapsible2" tabindex="0">Button 2</label>
<div class="collapsible-content">
<div class="content-inner">
<p>Test 2</p>
</div>
</div>
</div>