Last active
December 17, 2015 16:19
-
-
Save sdthornton/5638049 to your computer and use it in GitHub Desktop.
Small JS to add and remove the .hover class from the <html> tag during scrolling. Prepending the .hover class before all :hover or transition declarations in your css makes it so that hovers and transitions are only painted when not scrolling, which greatly increases page speed and scrolling smoothness.
This file contains hidden or 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 reducePaintOnScroll = (function() { | |
var enableTimer = 0; | |
window.addEventListener('scroll', function() { | |
clearTimeout(enableTimer); | |
removeHoverClass(); | |
enableTimer = setTimeout(addHoverClass, 200); | |
}, false); | |
function removeHoverClass() { | |
document.documentElement.classList.remove('hover'); | |
} | |
function addHoverClass() { | |
document.documentElement.classList.add('hover'); | |
} | |
return this; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example CSS to go with JS:
Rather than writing:
write: