Last active
March 29, 2016 14:42
-
-
Save simondell/e0df63a0a5163c2913da to your computer and use it in GitHub Desktop.
Another way to do hover states, using JavaScript. Normally I'd prefer CSS `:hover` or similar, but I wanted a way to move a `.selected` class around the DOM.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var hover = (function () { | |
var previousTarget = null; | |
return function (event) { | |
var currentTarget = event.srcElement; | |
if(currentTarget === previousTarget) return; | |
if(previousTarget) previousTarget.classList.remove('selected'); | |
if(currentTarget.nodeName.toLowerCase() === 'button') currentTarget.classList.add( 'selected' ); | |
previousTarget = currentTarget; | |
}; | |
})(); | |
document.addEventListener('mousemove', hover, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment