Created
May 29, 2013 15:28
-
-
Save jasonruyle/5671183 to your computer and use it in GitHub Desktop.
Disable hover on scroll (the anti-repaint).
Taken from http://www.html5rocks.com/en/tutorials/speed/unnecessary-paints/
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
/* Expect the hover class to be on the body | |
before doing any hover effects */ | |
.hover .block:hover { | |
… | |
} |
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
// Used to track the enabling of hover effects | |
var enableTimer = 0; | |
/* | |
* Listen for a scroll and use that to remove | |
* the possibility of hover effects | |
*/ | |
window.addEventListener('scroll', function() { | |
clearTimeout(enableTimer); | |
removeHoverClass(); | |
// enable after 1 second, choose your own value here! | |
enableTimer = setTimeout(addHoverClass, 1000); | |
}, false); | |
/** | |
* Removes the hover class from the body. Hover styles | |
* are reliant on this class being present | |
*/ | |
function removeHoverClass() { | |
document.body.classList.remove('hover'); | |
} | |
/** | |
* Adds the hover class to the body. Hover styles | |
* are reliant on this class being present | |
*/ | |
function addHoverClass() { | |
document.body.classList.add('hover'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment