File tree 1 file changed +28
-0
lines changed
1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -43,6 +43,8 @@ <h1>Slide in on Scroll</h1>
43
43
</ div >
44
44
45
45
< script >
46
+ // debounce is used to limit the frequency
47
+ // with which the contained function runs.
46
48
function debounce ( func , wait = 20 , immediate = true ) {
47
49
var timeout ;
48
50
return function ( ) {
@@ -58,6 +60,32 @@ <h1>Slide in on Scroll</h1>
58
60
} ;
59
61
}
60
62
63
+ const sliderImages = document . querySelectorAll ( '.slide-in' ) ;
64
+
65
+ function checkslide ( e ) {
66
+ // console.count(e);
67
+
68
+ sliderImages . forEach ( slideImage => {
69
+ // 1/3 of the way down the image
70
+ const slideInAt = ( ( window . scrollY + window . innerHeight ) - ( slideImage . height / 3 ) ) ;
71
+
72
+ // bottom of the image
73
+ const imageBottom = ( slideImage . offsetTop + slideImage . height ) ;
74
+
75
+ // comparison vars
76
+ const isHalfShown = slideInAt > slideImage . offsetTop ;
77
+ const isNotScrolledPast = window . scrollY < imageBottom ;
78
+
79
+ // comparison & add/remove class
80
+ if ( isHalfShown && isNotScrolledPast ) {
81
+ slideImage . classList . add ( 'active' ) ;
82
+ } else {
83
+ slideImage . classList . remove ( 'active' ) ;
84
+ }
85
+ } ) ;
86
+ }
87
+ window . addEventListener ( 'scroll' , debounce ( checkslide ) ) ;
88
+
61
89
</ script >
62
90
63
91
< style >
You can’t perform that action at this time.
0 commit comments