Created
October 1, 2014 19:32
-
-
Save nblenke/ca88e47a4861b6bb6e07 to your computer and use it in GitHub Desktop.
Fixed header on scroll
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
(function () { | |
var el = document.querySelector('#header'), | |
className = 'fixed', | |
offset = (el.offsetTop + el.offsetHeight), | |
scrollTop = function () { | |
return document.body.scrollTop || document.documentElement.scrollTop; | |
}; | |
if (scrollTop() > offset) { | |
el.className = className; | |
} | |
window.onscroll = function() { | |
if (scrollTop() > offset) { | |
if (!el.className.length) { | |
el.className = className; | |
} | |
} else { | |
if (el.className.length) { | |
el.className = ''; | |
} | |
} | |
}; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment