File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -58,6 +58,33 @@ <h1>Slide in on Scroll</h1>
5858 } ;
5959 }
6060
61+
62+ const sliderImages = document . querySelectorAll ( '.slide-in' ) ;
63+
64+ function checkSlide ( e ) {
65+ console . count ( e ) ;
66+ sliderImages . forEach ( sliderImage => {
67+ const slideInAt = ( window . scrollY + window . innerHeight ) - sliderImage . height / 2 ;
68+ //window.scrollY calculates from the top of the window
69+ //we want from the bottom of the window
70+ //pixel level where the image should be scrolled in
71+
72+ const imageBottom = sliderImage . offsetTop + sliderImage . height
73+ //offsetTop - top of this image is this far from top of window
74+ //add height of image to get the bottom of the img
75+
76+ const isHalfShown = slideInAt > sliderImage . offsetTop ;
77+ const isNotScrolledPast = window . scrollY < imageBottom ;
78+ if ( isHalfShown && isNotScrolledPast ) {
79+ sliderImage . classList . add ( 'active' ) ;
80+ } else {
81+ sliderImage . classList . remove ( 'active' ) ;
82+ }
83+ } ) ;
84+ }
85+
86+ window . addEventListener ( 'scroll' , debounce ( checkSlide ) ) ;
87+
6188 </ script >
6289
6390 < style >
You can’t perform that action at this time.
0 commit comments