Skip to content

Instantly share code, notes, and snippets.

@nash403
Last active October 21, 2016 07:58
Show Gist options
  • Save nash403/283c9b51621c1cbbd736c79e85133d9c to your computer and use it in GitHub Desktop.
Save nash403/283c9b51621c1cbbd736c79e85133d9c to your computer and use it in GitHub Desktop.
How to scroll fix an element
// The following code is all pseudo-code
let elem = ...; // get element we want to fix
let initScrollPosition = ...; // get initial position of element
// the following pseudo-code should be is in a scrolling listener
if (window.scrollYLevel > initScrollPosition) {
elem.css.position = 'fixed';
elem.css.top = 0; // 0 or other position we'd want
} else {
elem.css.position = 'static';
elem.css.top = 'auto'; // return back to the initial position
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment