كيف - قابلة للطي / الأكورديون
تعرف على كيفية إنشاء أكورديون (محتوى قابل للطي).
الأكورديون
تكون الأكورديونات مفيدة عندما تريد التبديل بين إخفاء كمية كبيرة من المحتوى وإظهارها:
الألم في حد ذاته مهم ، لكن الألم يزداد عن طريق عملية شد الدهون ، لكنني أعطيها الوقت لخفضها حتى أقوم ببعض الأعمال الرائعة والألم. لغرض الوصول إلى الحد الأدنى ، يجب على من منا ممارسة أي وظيفة باستثناء الاستفادة من العواقب المترتبة على ذلك.
الألم في حد ذاته مهم ، لكن الألم يزداد عن طريق عملية شد الدهون ، لكنني أعطيها الوقت لخفضها حتى أقوم ببعض الأعمال الرائعة والألم. لغرض الوصول إلى الحد الأدنى ، يجب على من منا ممارسة أي وظيفة باستثناء الاستفادة من العواقب المترتبة على ذلك.
الألم في حد ذاته مهم ، لكن الألم يزداد عن طريق عملية شد الدهون ، لكنني أعطيها الوقت لخفضها حتى أقوم ببعض الأعمال الرائعة والألم. لغرض الوصول إلى الحد الأدنى ، يجب على من منا ممارسة أي وظيفة باستثناء الاستفادة من العواقب المترتبة على ذلك.
إنشاء الأكورديون
الخطوة 1) أضف HTML:
مثال
<button class="accordion">Section 1</button>
<div class="panel">
<p>Lorem
ipsum...</p>
</div>
<button class="accordion">Section
2</button>
<div class="panel">
<p>Lorem ipsum...</p>
</div>
<button class="accordion">Section 3</button>
<div class="panel">
<p>Lorem
ipsum...</p>
</div>
الخطوة 2) أضف CSS:
نمط الأكورديون:
مثال
/* Style the buttons that are used to open and close the accordion panel */
.accordion {
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
text-align: left;
border: none;
outline: none;
transition: 0.4s;
}
/* Add a background color to the button if it is clicked on (add the
.active class with JS), and when you move the mouse over it (hover) */
.active, .accordion:hover {
background-color: #ccc;
}
/* Style the accordion panel. Note:
hidden by default */
.panel {
padding: 0 18px;
background-color: white;
display: none;
overflow: hidden;
}
الخطوة 3) أضف JavaScript:
مثال
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click",
function() {
/* Toggle between adding and removing the
"active" class,
to
highlight the button that controls the panel */
this.classList.toggle("active");
/* Toggle between hiding and showing the active panel */
var panel = this.nextElementSibling;
if (panel.style.display === "block") {
panel.style.display = "none";
}
else {
panel.style.display = "block";
}
});
}
الأكورديون المتحرك (التمرير لأسفل)
لعمل أكورديون متحرك ، أضف max-height: 0
و overflow: hidden
a transition
لخاصية max-height إلى panel
الفصل.
بعد ذلك ، استخدم JavaScript لتحريك المحتوى لأسفل عن طريق تعيين قيمة محسوبة
max-height
، اعتمادًا على ارتفاع اللوحة على أحجام الشاشات المختلفة:
مثال
<style>
.panel {
padding: 0 18px;
background-color: white;
max-height: 0;
overflow: hidden;
transition: max-height 0.2s ease-out;
}
</style>
<script>
var acc =
document.getElementsByClassName("accordion");
var i;
for (i = 0; i <
acc.length; i++) {
acc[i].addEventListener("click", function() {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.maxHeight) {
panel.style.maxHeight = null;
} else {
panel.style.maxHeight =
panel.scrollHeight + "px";
}
});
}
</script>
أضف الأيقونات
أضف رمزًا إلى كل زر للإشارة إلى ما إذا كان المحتوى القابل للطي مفتوحًا أم مغلقًا:
مثال
.accordion:after {
content: '\02795'; /* Unicode
character for "plus" sign (+) */
font-size: 13px;
color: #777;
float: right;
margin-left: 5px;
}
.active:after {
content: "\2796"; /*
Unicode character for "minus" sign (-) */
}