Skip to content

Instantly share code, notes, and snippets.

@beardedtim
Created June 22, 2022 14:01
Show Gist options
  • Save beardedtim/ffa89d03f8a3cbec821531ed2c6dc546 to your computer and use it in GitHub Desktop.
Save beardedtim/ffa89d03f8a3cbec821531ed2c6dc546 to your computer and use it in GitHub Desktop.
A way to have nested dropdown without any styling
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html, body {
width: 100%;
height: 100%;
}
.trigger {
cursor: pointer;
}
.dropdown {
visibility: hidden;
height: 0;
}
.shown {
visibility: visible;
height: auto;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Lesson 2</title>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<div id="app">
<ul>
<li>
<a href="#about">
About
</a>
</li>
<li>
<a href="#contact">
Contact
</a>
</li>
<li class="trigger">
Numbers
<ul class="dropdown">
<li>
<a href="#one">
One
</a>
</li>
<li>
<a href="#tw0">
two
</a>
</li>
<li class="trigger">
three
<ul class="dropdown">
<li>
<a href="#four">
four
</a>
</li>
<li>
<a href="#five">
five
</a>
</li>
<li>
<a href="#six">
six
</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</body>
<script defer src="index.js"></script>
</html>
window.addEventListener("click", (event) => {
if (event.target.classList.contains("trigger")) {
const dropdown = event.target.querySelector(".dropdown");
if (dropdown) {
dropdown.classList.toggle("shown");
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment