-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
40 lines (31 loc) · 1.57 KB
/
main.js
File metadata and controls
40 lines (31 loc) · 1.57 KB
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
const triggers = document.querySelectorAll('.cool > li'); // hover area complete li
const background = document.querySelector('.dropdownBackground');
const nav = document.querySelector('.top');
function mouseEnter(){
//first add the class to the <li></li>
this.classList.add('trigger-enter');
//load content only if 'trigger-enter exits
setTimeout(() => this.classList.contains('trigger-enter') && this.classList.add('trigger-enter-active'), 150);
//then add open class to background div (this class has opacity 1)
background.classList.add('open');
//get the coords for enclosing nav and dropdown
const dropdown = this.querySelector('.dropdown'); // not querySelectorAll because we want one at a time
const dropdownCoords = dropdown.getBoundingClientRect();
const navCoords = nav.getBoundingClientRect();
const divCoords = {
height: dropdownCoords.height, //match the content height width
width: dropdownCoords.width,
top: dropdownCoords.top - navCoords.top,
left: dropdownCoords.left - navCoords.left
}
background.style.setProperty('height',`${divCoords.height}px`)
background.style.setProperty('width', `${divCoords.width}px`)
background.style.setProperty('transform',`translate(${divCoords.left}px,${divCoords.top}px)`)
}
function mouseLeave(){
//remove classes
this.classList.remove('trigger-enter', 'trigger-enter-active');
background.classList.remove('open');
}
triggers.forEach(trigger => trigger.addEventListener('mouseenter',mouseEnter))
triggers.forEach(trigger => trigger.addEventListener('mouseleave',mouseLeave))