Use vanilla JavaScript and ES6 Class dynamic Multi-level sidebar navigation
- User
Use vanilla JavaScript and ES6 Class dynamic Multi-level sidebar navigation
- Assistant
Creating a multi-level dynamic sidebar navigation with vanilla JavaScript and ES6 classes requires the following steps:
- Create the HTML structure for the sidebar
- Create a CSS style for the sidebar elements
- Create an ES6 class to handle the sidebar's behavior
Let's go through each step in detail:
Step 1: Create the HTML structure for the sidebar
In this step, we'll create a sample HTML structure for a multi-level sidebar navigation:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="styles.css"> <title>Multi-Level Sidebar Navigation</title> </head> <body> <nav class="sidebar"> <ul> <li> <a href="#">Home</a> </li> <li> <a href="#">About</a> </li> <li> <a href="#">Services</a> <ul> <li><a href="#">Service 1</a></li> <li><a href="#">Service 2</a></li> <li><a href="#">Service 3</a></li> </ul> </li> <li> <a href="#">Contact</a> </li> </ul> </nav> <script src="script.js"></script> </body> </html>Step 2: Create a CSS style for the sidebar elements
In this step, we'll add some basic styling to the sidebar navigation:
/* styles.css */ body { font-family: sans-serif; } .sidebar { width: 200px; background-color: #333; position: fixed; height: 100%; } .sidebar ul { list-style-type: none; padding: 0; margin: 0; } .sidebar ul li { padding: 10px; } .sidebar ul li a { color: white; text-decoration: none; } .sidebar ul ul { display: none; } .sidebar ul li.active > ul { display: block; }Step 3: Create an ES6 class to handle the sidebar's behavior
In this step, we'll create a JavaScript class to manage the sidebar navigation's behavior:
// script.js class Sidebar { constructor(element) { this.element = element; this.init(); } init() { const itemsWithSubItems = Array.from(this.element.querySelectorAll("li > ul")).map(subItems => { return subItems.parentElement; }); itemsWithSubItems.forEach(item => { item.addEventListener('click', this.handleItemClick.bind(this, item)); }); } handleItemClick(item, event) { event.preventDefault(); if (item.classList.contains("active")) { item.classList.remove("active"); } else { item.classList.add("active"); } } } const sidebarElement = document.querySelector(".sidebar") const sidebar = new Sidebar(sidebarElement);Now, when you click a list item that contains sub-items, it will toggle their display, creating a multi-level sidebar navigation with vanilla JavaScript and ES6 classes.
- User
Multi-level sidebar navigation, 3th multi-level
- Assistant
If you want to create a multi-level sidebar navigation with a third level in the submenu, you can extend the HTML, CSS, and JavaScript code from before to accomplish this. Here's how you can do it:
Step 1: Modify the HTML structure
Add a third level of sub-items to the Services menu item:
... <li> <a href="#">Services</a> <ul> <li><a href="#">Service 1</a></li> <li> <a href="#">Service 2</a> <ul> <li><a href="#">Service 2.1</a></li> <li><a href="#">Service 2.2</a></li> </ul> </li> <li><a href="#">Service 3</a></li> </ul> </li> ...Step 2: Modify CSS styles
Update the CSS to properly hide, display, and indent the third-level sub-items:
... .sidebar ul ul ul { display: none; padding-left: 20px; } .sidebar ul li.active > ul li.active > ul { display: block; } ...Step 3: Modify the JavaScript
Update the JavaScript class to manage third-level menu items:
... init() { const itemsWithSubItems = Array.from(this.element.querySelectorAll("li > ul")).map(subItems => { return subItems.parentElement; }); itemsWithSubItems.forEach(item => { item.addEventListener('click', this.handleItemClick.bind(this, item)); }); this.element.addEventListener('click', (event) => { - if (event.target.tagName === 'A') { - event.stopPropagation(); - } }); } ...Now, you'll have a three-level sidebar navigation. When you click on the Service 2 menu item, it will reveal the third level of sub-items (Service 2.1 and Service 2.2).