Skip to content

Instantly share code, notes, and snippets.

@tayhimself
Created August 19, 2022 14:30
Show Gist options
  • Save tayhimself/50ccf1892a5f614ce2df12f1a967fff8 to your computer and use it in GitHub Desktop.
Save tayhimself/50ccf1892a5f614ce2df12f1a967fff8 to your computer and use it in GitHub Desktop.
Affix sidebar vanilla js
// 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