Last active
June 16, 2018 17:32
-
-
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.
This file contains hidden or 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
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