File tree 1 file changed +24
-0
lines changed
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>
58
58
} ;
59
59
}
60
60
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
+
61
85
</ script >
62
86
63
87
< style >
You can’t perform that action at this time.
0 commit comments