Skip to content

Instantly share code, notes, and snippets.

@mauryaratan
Last active June 16, 2018 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauryaratan/3fa527a86b2a7bb15047fec3543fa59f to your computer and use it in GitHub Desktop.
Save mauryaratan/3fa527a86b2a7bb15047fec3543fa59f to your computer and use it in GitHub Desktop.
Adds a top/bottom shadow for scrollable area depending on current scroll position.
const scrollable = document.querySelectorAll( '.scrollable' );
scrollable.forEach( function( el ) {
if ( el.offsetHeight < el.scrollHeight ) {
el.classList.add( 'bottom-shadow' );
}
el.addEventListener( 'scroll', function( e ) {
const scrollHeight = e.target.clientHeight;
const scrollPosition = scrollHeight + e.target.scrollTop;
const currentPosition = e.target.scrollHeight;
currentPosition === scrollPosition ? el.classList.remove( 'bottom-shadow' ) : el.classList.add( 'bottom-shadow' );
scrollHeight === scrollPosition ? el.classList.remove( 'top-shadow' ) : el.classList.add( 'top-shadow' );
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment