var topPos; var updatedPos; window.onscroll = navPos; if (!topPos) { topPos = 124; } function navPos() { var pos = window.scrollY; if (!topPos) { topPos = 124; } var st = document.getElementById('navlinks'); if (st) { if (pos < topPos && updatedPos != 'absolute') { st.style.position = 'absolute'; st.style.top = topPos + 'px'; updatedPos = 'absolute'; } else if (pos >= topPos && updatedPos != 'fixed') { st.style.position = 'fixed'; st.style.top = '0'; updatedPos = 'fixed'; } } } navPos(); //Thanks shinjuo and Adam Lynch!