Skip to content

Commit 038a120

Browse files
committed
Day 26 complete
1 parent 00a492c commit 038a120

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

26 - Stripe Follow Along Nav/index.html

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,49 @@ <h2>Cool</h2>
208208
</style>
209209

210210
<script>
211-
211+
const triggers = document.querySelectorAll('.cool > li');
212+
const background = document.querySelector('.dropdownBackground');
213+
const nav = document.querySelector('.top');
214+
215+
function handleEnter(event) {
216+
// by passing in the event as a param and grabbing its target we can avoid
217+
// all the messiness of `this`
218+
const me = event.target;
219+
me.classList.add('trigger-enter');
220+
setTimeout(() => {
221+
me.classList.contains('trigger-enter') &&
222+
me.classList.add('trigger-enter-active')
223+
}, 150);
224+
background.classList.add('open');
225+
226+
const dropdown = me.querySelector('.dropdown');
227+
const dropdownCoords = dropdown.getBoundingClientRect();
228+
const navCoords = nav.getBoundingClientRect();
229+
230+
const coords = {
231+
height: dropdownCoords.height,
232+
width: dropdownCoords.width,
233+
top: dropdownCoords.top - navCoords.top,
234+
left: dropdownCoords.left - navCoords.left
235+
};
236+
237+
background.style.setProperty('width', `${coords.width}px`);
238+
background.style.setProperty('height', `${coords.height}px`);
239+
background.style.setProperty('transform', `translate(${coords.left}px, ${coords.top}px)`);
240+
}
241+
242+
function handleLeave(event) {
243+
// by passing in the event as a param and grabbing its target we can avoid
244+
// all the messiness of `this`
245+
const me = event.target;
246+
me.classList.remove('trigger-enter', 'trigger-enter-active');
247+
background.classList.remove('open');
248+
}
249+
250+
triggers.forEach(trigger => {
251+
trigger.addEventListener('mouseenter', handleEnter);
252+
trigger.addEventListener('mouseleave', handleLeave);
253+
});
212254

213255
</script>
214256

0 commit comments

Comments
 (0)