Created
August 19, 2022 14:30
-
-
Save tayhimself/50ccf1892a5f614ce2df12f1a967fff8 to your computer and use it in GitHub Desktop.
Affix sidebar vanilla js
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
// element height that we want to scroll | |
let elheight = document.getElementById("check-sidebar").offsetHeight | |
window.addEventListener('scroll', (e) => { | |
//last_known_scroll_position = window.scrollY; | |
let scrollTop = window.pageYOffset; | |
if( scrollTop > elheight ){ | |
document.getElementById('check-sidebar').style.top="40px"; | |
document.getElementById('check-sidebar').style.position="fixed"; | |
} | |
else | |
{ | |
document.getElementById('check-sidebar').style.position="relative"; | |
document.getElementById('check-sidebar').style.top="50px"; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment