कॉलैप्सिबल सेक्शन (अकॉर्डियन) वेब पेज पर कंटेंट को व्यवस्थित करने का एक शानदार तरीका है, जिससे उपयोगकर्ता केवल वही देखते हैं जिसमें वे रुचि रखते हैं।
उदाहरण
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style>
body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; }
.collapse { background-color: #007bff; color: white; cursor: pointer; padding: 18px; width: 100%; border: none; text-align: left; outline: none; font-size: 18px; transition: background 0.3s; }
.active, .collapse:hover { background-color: #0056b3; }
.content { padding: 0 18px; display: none; overflow: hidden; background-color: #f8f9fa; font-size: 18px; max-height: 0; transition: max-height 0.3s ease-out; }
.content.show { display: block; max-height: 500px; transition: max-height 0.5s ease-in; }
</style> </head> <body>
<h1>कॉलैप्सिबल उदाहरण</h1>
<h2>नीचे दिए गए कॉलैप्सिबल पर क्लिक करके कंटेंट दिखाएं</h2>
<button type="button" class="collapse">कॉलैप्सिबल खोलें</button>
<div class="content">
<h3>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</h3>
<p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
<script>
const collapse = document.querySelector(".collapse");
const content = collapse.nextElementSibling;
collapse.addEventListener("click", function() {
this.classList.toggle("active");
content.classList.toggle("show");
});
</script> </body> </html>
आउटपुट
प्रारंभ में केवल बटन दिखाई देता है। क्लिक करने पर कंटेंट स्लाइड डाउन एनिमेशन के साथ खुलता है, और फिर से क्लिक करने पर स्लाइड अप होकर बंद हो जाता है।
बहु-कॉलैप्सिबल के लिए:
document.querySelectorAll(".collapse").forEach(btn => {
btn.addEventListener("click", function() {
this.classList.toggle("active");
const content = this.nextElementSibling;
content.classList.toggle("show");
});
});
एक्सेसिबिलिटी सुधार: aria-expanded और aria-controls एट्रिब्यूट्स जोड़ें ताकि स्क्रीन रीडर्स के लिए बेहतर सपोर्ट मिले।