Skip to content

Instantly share code, notes, and snippets.

@simondell
Last active March 29, 2016 14:42
Show Gist options
  • Save simondell/e0df63a0a5163c2913da to your computer and use it in GitHub Desktop.
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.
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