$(".summary_1").click(function(){
if ($("#hotel-facilities").is(":hidden")) {
$("#hotel-facilities").slideDown("slow");
$(".summary_1").addClass("open");
} else {
$("#hotel-facilities").hide("slow");
$(".summary_1").removeClass("open");
}
});
$(".summary_2").click(function(){
if ($("#lodging_type").is(":hidden")) {
$("#lodging_type").slideDown("slow");
$(".summary_2").addClass("open");
} else {
$("#lodging_type").hide("slow");
$(".summary_2").removeClass("open");
}
});
.details {
border-top: 1px solid #cdd0d2;
border-bottom: 0;
cursor: pointer;
}
.tab_content {
display: none;
position: relative;
border-bottom: 1px solid #cdd0d2;
width: 250px;
user-select: none;
outline: 0;
}
.is-hidden {
display: none;
}
.summary_1, .summary_2 {
font-size: 14px;
padding: 14px 40px;
position: relative;
list-style-type: none;
margin: 0;
font-weight: 400;
}
.summary_1:before {
/*-webkit-transform: translate(-50%,-50%) rotate(0);*/
transform: translate(-50%,-50%) rotate(-90deg);
will-change: translate,rotate;
}
.summary_2:before {
/*-webkit-transform: translate(-50%,-50%) rotate(0);*/
transform: translate(-50%,-50%) rotate(-90deg);
will-change: translate,rotate;
}
.summary_1:after {
transform: translate(-50%,-50%);
will-change: translate,opacity;
}
.summary_2:after {
transform: translate(-50%,-50%);
will-change: translate,opacity;
}
.summary_1.open:before{ /* добавочный класс */
transform:translate(-50%,-50%) rotate(-0deg);
width:0;
}
.summary_2.open:before{ /* добавочный класс */
transform:translate(-50%,-50%) rotate(-0deg);
width:0;
}
.summary_1:after,
.summary_1:before {
content: "";
width: 14px;
height: 2px;
border-radius: 2px;
background-color: #37454d;
display: block;
position: absolute;
top: 50%;
transition: all .2s ease-in-out;
opacity: 1;
left: 21px;
contain: strict;
}
.summary_2:after,
.summary_2:before {
content: "";
width: 14px;
height: 2px;
border-radius: 2px;
background-color: #37454d;
display: block;
position: absolute;
top: 50%;
transition: all .2s ease-in-out;
opacity: 1;
left: 21px;
contain: strict;
}
.summary_1, .summary_2:hover {
background: #ebeced;
cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="details">
<h4 class="summary_1" role="tab" aria-expanded="false" aria-owns="hotel-facilities" aria-controls="hotel-facilities" tabindex="0">Hotel facilities</h4>
<div id="hotel-facilities" class="is-hidden">
<div role="treeitem" aria-selected="false">
<label class="checkbox-container grouped">
<input type="checkbox" id="hotel-facilities-200" name="hotel-facilities-200" class="checkbox">
<span class="label">24-hour reception (4923)</span>
</label>
</div>
<div role="treeitem" aria-selected="false">
<label class="checkbox-container grouped">
<input type="checkbox" id="hotel-facilities-200" name="hotel-facilities-200" class="checkbox">
<span class="label">24-hour room service (286)</span>
</label>
</div>
<div role="treeitem" aria-selected="false">
<label class="checkbox-container grouped">
<input type="checkbox" id="hotel-facilities-200" name="hotel-facilities-200" class="checkbox">
<span class="label">Airport shuttle (602)</span>
</label>
</div>
<div role="treeitem" aria-selected="false">
<label class="checkbox-container grouped">
<input type="checkbox" id="hotel-facilities-200" name="hotel-facilities-200" class="checkbox">
<span class="label">Beach umbrellas (2094)</span>
</label>
</div>
<div role="treeitem" aria-selected="false">
<label class="checkbox-container grouped">
<input type="checkbox" id="hotel-facilities-200" name="hotel-facilities-200" class="checkbox">
<span class="label">Bungalows (152)</span>
</label>
</div>
<div role="treeitem" aria-selected="false">
<label class="checkbox-container grouped">
<input type="checkbox" id="hotel-facilities-200" name="hotel-facilities-200" class="checkbox">
<span class="label">Business center (3052)</span>
</label>
</div>
<div role="treeitem" aria-selected="false">
<label class="checkbox-container grouped">
<input type="checkbox" id="hotel-facilities-200" name="hotel-facilities-200" class="checkbox">
<span class="label">Casino (113)</span>
</label>
</div>
<div role="treeitem" aria-selected="false">
<label class="checkbox-container grouped">
<input type="checkbox" id="hotel-facilities-200" name="hotel-facilities-200" class="checkbox">
<span class="label">Concierge (2187)</span>
</label>
</div>
<div role="treeitem" aria-selected="false">
<label class="checkbox-container grouped">
<input type="checkbox" id="hotel-facilities-200" name="hotel-facilities-200" class="checkbox">
<span class="label">Conference rooms (2659)</span>
</label>
</div>
<div role="treeitem" aria-selected="false">
<label class="checkbox-container grouped">
<input type="checkbox" id="hotel-facilities-200" name="hotel-facilities-200" class="checkbox">
<span class="label">Convenience store (1219)</span>
</label>
</div>
</div>
</section>
<section class="details">
<h4 class="summary_2" role="tab" aria-expanded="false" aria-owns="hotel-facilities" aria-controls="hotel-facilities" tabindex="0">Type of lodging</h4>
<div id="lodging_type" class="is-hidden">
<div role="treeitem" aria-selected="false">
<label class="checkbox-container grouped">
<input type="checkbox" id="lodging_type-200" name="lodging_type-200" class="checkbox">
<span class="label">Bed & Breakfast (427)</span>
</label>
</div>
<div role="treeitem" aria-selected="false">
<label class="checkbox-container grouped">
<input type="checkbox" id="lodging_type-200" name="lodging_type-200" class="checkbox">
<span class="label">Camping Site (27)</span>
</label>
</div>
<div role="treeitem" aria-selected="false">
<label class="checkbox-container grouped">
<input type="checkbox" id="lodging_type-200" name="lodging_type-200" class="checkbox">
<span class="label">Hostel (121)</span>
</label>
</div>
<div role="treeitem" aria-selected="false">
<label class="checkbox-container grouped">
<input type="checkbox" id="lodging_type-200" name="lodging_type-200" class="checkbox">
<span class="label">Hotel (8967)</span>
</label>
</div>
<div role="treeitem" aria-selected="false">
<label class="checkbox-container grouped">
<input type="checkbox" id="lodging_type-200" name="lodging_type-200" class="checkbox">
<span class="label">Motel (816)</span>
</label>
</div>
<div role="treeitem" aria-selected="false">
<label class="checkbox-container grouped">
<input type="checkbox" id="lodging_type-200" name="lodging_type-200" class="checkbox">
<span class="label">Pension (243)</span>
</label>
</div>
<div role="treeitem" aria-selected="false">
<label class="checkbox-container grouped">
<input type="checkbox" id="lodging_type-200" name="lodging_type-200" class="checkbox">
<span class="label">Serviced Apartment (493)</span>
</label>
</div>
<div role="treeitem" aria-selected="false">
<label class="checkbox-container grouped">
<input type="checkbox" id="lodging_type-200" name="lodging_type-200" class="checkbox">
<span class="label">Vacation Home (3417)</span>
</label>
</div>
<div role="treeitem" aria-selected="false">
<label class="checkbox-container grouped">
<input type="checkbox" id="lodging_type-200" name="lodging_type-200" class="checkbox">
<span class="label">Vacation Rental (11847)</span>
</label>
</div>
</div>
</section>
大家好,有 2 个块,单击它们会打开。我为每个块编写了自己的类和自己的标识符。我需要以某种方式组合这些块。这样就没有重复的代码。为什么?因为该站点将有 9这样的块并为每个等编写summary_1, summary_2, summary_3, summary_4 ,summary_5并错误地给出你的风格,你需要有一个单一的类,如果不可能的话,你需要一个单一的标识符,这样代码就不会以这种方式重复。一切都写得优雅而正确。
如果你自己看到类是一样的,工作的逻辑是一样的,那就留下一个类就行了。