![]()
Implementing a Collapsible Sidebar Using jQuery UI – A Complete Guide
Introduction
A collapsible sidebar is a great way to enhance a website’s user experience by providing easy navigation while saving screen space. This feature is commonly used in admin dashboards, web applications, and mobile-friendly websites.
In this detailed guide, we will explore how to create, customize, and optimize a collapsible sidebar using jQuery UI.
Table of Contents
1️⃣ What is a Collapsible Sidebar?
2️⃣ Why Use jQuery UI for a Collapsible Sidebar?
3️⃣ Setting Up jQuery UI for Sidebar Functionality
4️⃣ Creating a Basic Collapsible Sidebar
5️⃣ Adding Expand and Collapse Animations
6️⃣ Styling the Sidebar with CSS
7️⃣ Using jQuery UI Effects for Smooth Transitions
8️⃣ Controlling Sidebar Visibility with Toggle Button
9️⃣ Implementing a Persistent Sidebar State
🔟 Making the Sidebar Responsive for Mobile Devices
📌 Handling Sidebar Events and Callbacks
📌 Adding Icons and Visual Enhancements
📌 Loading Sidebar Content Dynamically with AJAX
📌 Optimizing Performance for Large Websites
📌 Best Practices for Collapsible Sidebars
📌 Conclusion
1. What is a Collapsible Sidebar?
A collapsible sidebar is a vertical navigation panel that can be expanded or collapsed to save space. It is commonly used for:
✔ Navigation menus in admin dashboards
✔ Settings panels in web applications
✔ Filtering options in eCommerce sites
✔ Content organization in documentation sites
2. Why Use jQuery UI for a Collapsible Sidebar?
Using jQuery UI provides smooth animations, better compatibility, and customizable effects compared to plain JavaScript or CSS-only solutions.
✔ Key Benefits of jQuery UI for Sidebars:
✅ Smooth Animations – Slide, fade, and custom effects
✅ Easy Event Handling – Toggle, open, close, hover effects
✅ AJAX Integration – Load content dynamically
✅ Customizable Themes – Easily modify appearance
✅ Cross-Browser Support – Works in all modern browsers
3. Setting Up jQuery UI for Sidebar Functionality
Step 1: Include jQuery and jQuery UI
To use jQuery UI, include the following CDN links in your HTML file:
<!-- jQuery & jQuery UI (CDN) -->
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
4. Creating a Basic Collapsible Sidebar
Step 2: Add HTML for Sidebar
<div id="sidebar">
<h3>Menu</h3>
<ul>
<li><a href="#">Dashboard</a></li>
<li><a href="#">Profile</a></li>
<li><a href="#">Settings</a></li>
<li><a href="#">Logout</a></li>
</ul>
</div>
<button id="toggleSidebar">Toggle Sidebar</button>
5. Adding Expand and Collapse Animations
Step 3: Add jQuery UI Script for Toggle Effect
$(document).ready(function () {
$("#toggleSidebar").click(function () {
$("#sidebar").toggle("slide", { direction: "left" }, 500);
});
});
✔ The toggle("slide") method uses jQuery UI’s slide effect to smoothly open and close the sidebar.
✔ The { direction: "left" } option makes the sidebar slide horizontally.
✔ 500 is the duration in milliseconds.
6. Styling the Sidebar with CSS
#sidebar {
width: 250px;
height: 100vh;
position: fixed;
left: 0;
top: 0;
background: #2c3e50;
color: white;
padding: 20px;
box-shadow: 2px 0px 5px rgba(0, 0, 0, 0.2);
}
#sidebar ul {
list-style: none;
padding: 0;
}
#sidebar ul li {
padding: 10px 0;
}
#sidebar ul li a {
color: white;
text-decoration: none;
}
#toggleSidebar {
position: absolute;
top: 20px;
left: 260px;
background: #3498db;
color: white;
border: none;
padding: 10px;
cursor: pointer;
}
✔ Fixed Positioning: Ensures the sidebar stays on the left.
✔ Box Shadow: Adds a subtle 3D effect.
✔ Button Styling: Improves visibility and usability.
7. Using jQuery UI Effects for Smooth Transitions
We can replace the slide effect with fade or bounce animations.
$("#toggleSidebar").click(function () {
$("#sidebar").toggle("fade", 500); // Fades in and out
});
✔ toggle("fade") gives a smooth fade-in/out effect.
$("#toggleSidebar").click(function () {
$("#sidebar").toggle("bounce", { times: 3 }, 500);
});
✔ toggle("bounce") makes the sidebar bounce before disappearing.
8. Controlling Sidebar Visibility with Toggle Button
To detect sidebar state, use:
$("#toggleSidebar").click(function () {
if ($("#sidebar").is(":visible")) {
$("#sidebar").hide("slide", { direction: "left" }, 500);
$(this).text("Show Sidebar");
} else {
$("#sidebar").show("slide", { direction: "left" }, 500);
$(this).text("Hide Sidebar");
}
});
✔ This changes the button text dynamically!
9. Implementing a Persistent Sidebar State
To save sidebar state across page reloads, use localStorage.
$(document).ready(function () {
if (localStorage.getItem("sidebar") === "hidden") {
$("#sidebar").hide();
}
$("#toggleSidebar").click(function () {
$("#sidebar").toggle("slide", { direction: "left" }, 500);
let state = $("#sidebar").is(":visible") ? "visible" : "hidden";
localStorage.setItem("sidebar", state);
});
});
✔ The sidebar remembers its last state even after refreshing the page!
10. Making the Sidebar Responsive for Mobile Devices
For mobile-friendly behavior, adjust width and position.
@media (max-width: 768px) {
#sidebar {
width: 200px;
}
#toggleSidebar {
left: 210px;
}
}
✔ Ensures the sidebar looks good on all devices!
11. Handling Sidebar Events and Callbacks
To trigger actions when the sidebar opens or closes, use callbacks:
$("#sidebar").toggle("slide", { direction: "left" }, 500, function () {
console.log("Sidebar toggled!");
});
✔ Useful for logging events, tracking user actions, or triggering animations.
12. Adding Icons and Visual Enhancements
Use Font Awesome icons for a better look:
<li><i class="fas fa-home"></i> <a href="#">Dashboard</a></li>
✔ Improves visual clarity!
🎯 A collapsible sidebar enhances UX and improves navigation efficiency.
By using jQuery UI, we can create a responsive, smooth, and customizable sidebar with animations, local storage support, and AJAX content loading!
