Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update script.js #29

Merged
merged 1 commit into from Apr 24, 2020
Merged

Update script.js #29

merged 1 commit into from Apr 24, 2020

Conversation

Novailoveyou
Copy link
Contributor

I added functionality to the navbar so it closes when the user clicks anywhere outside of the navbar. My code is messy, but it works and it does not leave unused event listeners on when we don't want them.

@bradtraversy bradtraversy merged commit 871c48a into bradtraversy:master Apr 24, 2020
@olaf-wilkosz
Copy link

olaf-wilkosz commented Feb 11, 2022

I like your update a lot and I have worked around to clean it a little to fit the rest of script with the ternary operator and that is my outcome:

document.body.addEventListener('click', e => {
	document.body.classList.contains('show-nav') &&
	e.target != nav &&
	!nav.contains(e.target) &&
	e.target != toggle &&
	!toggle.contains(e.target)
		? document.body.classList.toggle('show-nav')
		: false;
});

I am still not sure what about unused event listeners because that is the first time when I see someone removing them :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants