Welcome to this comprehensive guide on creating an Accordion Widget using jQuery! This tutorial is designed for both beginners and intermediate learners. By the end of this lesson, you'll have a solid understanding of how to create and customize accordions to enhance your web projects. 📝 Note: This tutorial assumes you have a basic understanding of HTML and CSS. If not, we recommend checking out our HTML and CSS tutorials first.
An accordion widget is a user interface component that allows users to expand and collapse content areas, revealing or hiding the information within. It's a great way to save space and organize complex content in a clean and efficient manner.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Accordion Widget</title>
</head>
<body>
<!-- Accordion section will be added here -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<!-- Add your custom JavaScript here -->
</body>
</html>Let's create a simple accordion structure with two sections.
<div id="accordion">
<div class="accordion-item">
<header class="accordion-header">
<h2 class="accordion-title">Section 1</h2>
<span class="accordion-icon">►</span>
</header>
<div class="accordion-content">
<p>Content for Section 1.</p>
</div>
</div>
<div class="accordion-item">
<header class="accordion-header">
<h2 class="accordion-title">Section 2</h2>
<span class="accordion-icon">▼</span>
</header>
<div class="accordion-content">
<p>Content for Section 2.</p>
</div>
</div>
</div>Now, let's add the jQuery code to make the accordion functional.
$(document).ready(function() {
$('.accordion-header').click(function() {
$(this).siblings('.accordion-content').slideToggle();
$(this).find('.accordion-icon').toggleClass('rotate');
});
});Explanation: We're using jQuery's .ready() function to ensure the DOM is loaded before executing the code. Then, we're adding a click event listener to the .accordion-header. When a header is clicked, we toggle the visibility of the corresponding .accordion-content using the .slideToggle() function and change the rotation of the accordion icon using the .toggleClass() function.
Add some basic CSS to style the accordion.
body {
font-family: Arial, sans-serif;
}
.accordion {
width: 100%;
}
.accordion-item {
border-bottom: 1px solid #ccc;
padding: 20px;
}
.accordion-title {
margin: 0;
}
.accordion-icon {
display: inline-block;
font-size: 20px;
transition: transform 0.3s;
}
.accordion-icon.rotate {
transform: rotate(90deg);
}
.accordion-content {
display: none;
}To make sections collapsible by default, add the collapsed class to the .accordion-item. Then, update the JavaScript code to hide the content by default.
$(document).ready(function() {
$('.accordion-header').click(function() {
$(this).parent().find('.accordion-content').slideToggle();
$(this).find('.accordion-icon').toggleClass('rotate');
});
});<div class="accordion-item collapsed">
<!-- Accordion section content here -->
</div>Explanation: When a collapsed section is clicked, we find the corresponding .accordion-content and slide it up, making it visible.
To add an active state to the currently open section, update the JavaScript code.
$(document).ready(function() {
var openSection;
$('.accordion-header').click(function(event) {
event.preventDefault();
if (openSection) {
$(openSection).find('.accordion-icon').toggleClass('rotate');
$(openSection).find('.accordion-content').slideToggle();
}
openSection = $(this).parent();
$(this).find('.accordion-icon').toggleClass('rotate');
$(this).parent().find('.accordion-content').slideToggle();
});
});Explanation: We're storing the currently open section in the openSection variable. When a new section is clicked, we toggle the active state of the previous section (if any) and open the new section.
Which jQuery function is used to toggle the visibility of an element?
That's it for this tutorial! You now have a solid foundation for creating and customizing accordion widgets using jQuery. Keep practicing, and happy coding! 🎯🎉