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