A pretty simple JS include to use when you don’t already have jQuery or the like included on a page. Just adds a data-nav=”fixed” attribute to your body element that you can style off of via body[data-nav=”fixed”] .whatever {} — this also has the benefit of not hardcoding any styles — you’re free to do it all via your CSS files.
Just remember to swap out ‘nav’ for whatever ID or selector you’ve got on your actual nav.
Potential optimizations would be not using document.getElementById over and over, and just leaving the element cached in a global. I would advise against caching nav’s offsetTop, though — as it’s possible that things may change in the dom, and that could change!
window.onscroll = function() {
var d = document,
nav = d.getElementById( 'nav' ),
att = ( window.pageYOffset > d.getElementById( 'nav' ).offsetTop ) ? 'fixed' : 'normal';
d.body.setAttribute( 'data-nav', att );
}
