-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
44 lines (38 loc) · 1.22 KB
/
index.js
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
const menu = document.querySelector(".hamburger");
const sidebar = document.querySelector(".sidebar");
const closeBtn = document.querySelector(".close-btn");
// opens sidebar when hamburger menu is clicked
menu.addEventListener("click", (event) => {
event.stopPropagation();
sidebar.classList.toggle("open");
setSidebarHeight();
});
// stop event from bubbling up to the body
sidebar.addEventListener("click", (event) => {
event.stopPropagation();
});
// closes sidebar when body outside sidebar is clicked
document.body.addEventListener("click", () => {
if (sidebar.classList.contains("open")) {
sidebar.classList.remove("open");
}
});
// closes sidebar when "X" button is clicked
closeBtn.addEventListener("click", () => {
if (sidebar.classList.contains("open")) {
sidebar.classList.remove("open");
}
});
// sets sidebar to full size of content on short screens
window.addEventListener("resize", () => {
setSidebarHeight();
});
function setSidebarHeight() {
const size = document.body.getBoundingClientRect();
const windowHeight = window.innerHeight;
if (size.width <= 740 && windowHeight <= 610) {
sidebar.style.minHeight = size.height + "px";
} else {
sidebar.style = "";
}
};