File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -43,7 +43,7 @@ <h1>Slide in on Scroll</h1>
4343 </ div >
4444
4545 < script >
46- function debounce ( func , wait = 20 , immediate = true ) {
46+ function debounce ( func , wait = 15 , immediate = true ) {
4747 var timeout ;
4848 return function ( ) {
4949 var context = this , args = arguments ;
@@ -58,6 +58,29 @@ <h1>Slide in on Scroll</h1>
5858 } ;
5959 }
6060
61+ const sliderImages = document . querySelectorAll ( '.slide-in' ) ;
62+
63+ // window.scrollY = how much page is scrolled down at top of screen
64+ // window.scrollY + window.innerHeight = how much page is scrolled down at bottom of screen
65+ function checkSlide ( e ) {
66+ sliderImages . forEach ( sliderImage => {
67+ // Half way through the image
68+ const slideInAt = ( window . scrollY + window . innerHeight ) - ( sliderImage . height / 2 ) ;
69+ // offsetTop = How far the top of image is from top of window (add image height to get the value for the bottom)
70+ const imageBottom = sliderImage . offsetTop + sliderImage . height ;
71+ const isHalfShown = slideInAt > sliderImage . offsetTop ;
72+ const isNotScrolledPast = window . scrollY < imageBottom ;
73+
74+ if ( isHalfShown && isNotScrolledPast ) {
75+ sliderImage . classList . add ( 'active' ) ;
76+ } else {
77+ sliderImage . classList . remove ( 'active' ) ;
78+ }
79+ } ) ;
80+ }
81+
82+ window . addEventListener ( 'scroll' , debounce ( checkSlide ) ) ;
83+
6184 </ script >
6285
6386 < style >
You can’t perform that action at this time.
0 commit comments