-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs-simple-dropdown.html
64 lines (54 loc) · 2.23 KB
/
js-simple-dropdown.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<meta charset="utf-8">
<div class="dropdown">
<h3 class="dropdown__title">Expanding Element Title</h3>
<p class="dropdown__body">Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Aenean lacinia bibendum nulla sed consectetur.</p>
</div>
<div class="dropdown">
<h3 class="dropdown__title">Expanding Element Title</h3>
<p class="dropdown__body">Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Aenean lacinia bibendum nulla sed consectetur.</p>
</div>
<div class="dropdown">
<h3 class="dropdown__title">Expanding Element Title</h3>
<p class="dropdown__body">Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Aenean lacinia bibendum nulla sed consectetur.</p>
</div>
<div class="dropdown">
<h3 class="dropdown__title">Expanding Element Title</h3>
<p class="dropdown__body">Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Aenean lacinia bibendum nulla sed consectetur.</p>
</div>
<script>
// Get all elements with the 'dropdown' class
// Close an .open dropdown on click
// Close all other dropdowns when a new one is opened
// Reveal content within dropdown element
const dropdowns = document.querySelectorAll('.dropdown')
dropdowns.forEach(el => {
const isOpen = () => el.classList.contains('open')
const open = () => el.classList.add('open')
const close = (el) => el.classList.remove('open')
el.addEventListener('click', () => {
isOpen() ? close(el) : (dropdowns.forEach(el => close(el)), open())
})
})
</script>
<style>
.dropdown {
position: relative;
cursor: pointer;
}
.dropdown::after {
content: "➕";
position: absolute;
top: 0;
right: 1rem;
}
.dropdown.open::after {
content: "➖";
}
.dropdown__body {
height: 0;
overflow: hidden;
}
.dropdown.open .dropdown__body {
height: auto;
}
</style>