Skip to content

Instantly share code, notes, and snippets.

@rickd-uk
Created February 6, 2022 02:33
Show Gist options
  • Save rickd-uk/d950bdd2767ccd403b43cf15d7eafbbc to your computer and use it in GitHub Desktop.
Save rickd-uk/d950bdd2767ccd403b43cf15d7eafbbc to your computer and use it in GitHub Desktop.
JS - Delayed Event (Mouseover, Mouseout)
function myDelayedThing() {
var mySelectors = document.querySelectorAll('.something');
// Loop through mySelectors
for(var i = 0; i < menuLinks.length; i++) {
// Add 'open' class on mouseover
menuLinks[i].addEventListener('mouseover', function() {
this.classList.add('open');
});
// Remove 'open" class with a delay
menuLinks[i].addEventListener('mouseout', function () {
var node = this; // Allows us to access 'this' within the timeout function
setTimeout(function() {
node.classList.remove('open');
}, 500)
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment