File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -58,6 +58,27 @@ <h1>Slide in on Scroll</h1>
5858 } ;
5959 }
6060
61+ const sliderImages = document . querySelectorAll ( '.slide-in' ) ;
62+
63+ function checkSlide ( e ) {
64+ sliderImages . forEach ( img => {
65+ const slideInAt = ( window . scrollY + window . innerHeight ) - img . height / 2 ; // Half way of image.
66+ const imageBottom = img . offsetTop + img . height ; // Bottom of image.
67+
68+ const isHalfShown = slideInAt > img . offsetTop ;
69+ const isNotScrolledPast = window . scrollY < imageBottom ;
70+
71+ if ( isHalfShown && isNotScrolledPast ) {
72+ img . classList . add ( 'active' ) ;
73+ } else {
74+ img . classList . remove ( 'active' ) ;
75+ }
76+ } )
77+ }
78+
79+ window . addEventListener ( 'scroll' , debounce ( checkSlide ) ) ;
80+
81+
6182 </ script >
6283
6384 < style >
You can’t perform that action at this time.
0 commit comments