-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path26.js
46 lines (41 loc) · 1.58 KB
/
26.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
45
46
function followAlongNav() {
const triggers = document.querySelectorAll(".cool > li");
const background = document.querySelector(".dropdownBackground");
const nav = document.querySelector(".top");
function handleEnter() {
this.classList.add("trigger-enter");
setTimeout(
() =>
this.classList.contains("trigger-enter") &&
this.classList.add("trigger-enter-active"),
150
);
background.classList.add("open");
const dropdown = this.querySelector(".dropdown");
const dropdownCoords = dropdown.getBoundingClientRect();
const navCoords = nav.getBoundingClientRect();
const coords = {
height: dropdownCoords.height,
width: dropdownCoords.width,
top: dropdownCoords.top - navCoords.top,
left: dropdownCoords.left - navCoords.left,
};
background.style.setProperty("width", `${coords.width}px`);
background.style.setProperty("height", `${coords.height}px`);
background.style.setProperty(
"transform",
`translate(${coords.left}px,${coords.top}px)`
);
}
function handleLeave() {
this.classList.remove("trigger-enter", "trigger-enter-active");
background.classList.remove("open");
}
triggers.forEach((trigger) =>
trigger.addEventListener("mouseenter", handleEnter)
);
triggers.forEach((trigger) =>
trigger.addEventListener("mouseleave", handleLeave)
);
}
window.addEventListener("DOMContentLoaded", followAlongNav);