File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -58,6 +58,45 @@ <h1>Slide in on Scroll</h1>
5858 } ;
5959 }
6060
61+ //image is halfscrolled it should be active
62+ //image is scrolled past it should be hidden
63+
64+
65+ /*
66+ how do you get if image is half scrolled
67+ * check for the image offsetTop and calculate if the scroll postion is past that offsetTop
68+ * scroll position is calculated by checking the ScrollY position of the window + the window height
69+ * and the image half position
70+
71+ */
72+
73+ /*
74+ how do you get if image is scrolled past
75+ * get the image bottom position
76+ * check if the window scrollY is past that image bottom position
77+
78+ */
79+
80+
81+ const sliderImages = document . querySelectorAll ( '.slide-in' ) ;
82+ window . addEventListener ( 'scroll' , debounce ( checkSlide ) ) ;
83+
84+ function checkSlide ( e ) {
85+ sliderImages . forEach ( sliderImage => {
86+ const slideInAt = ( window . scrollY + window . innerHeight ) - ( sliderImage . height / 2 ) ;
87+ const isHalfScrolled = slideInAt > sliderImage . offsetTop ;
88+
89+ const imageBottom = sliderImage . offsetTop + sliderImage . height ;
90+ const isScrolledPast = window . scrollY > imageBottom ;
91+ if ( isHalfScrolled && ! isScrolledPast ) {
92+ sliderImage . classList . add ( 'active' ) ;
93+ }
94+ else {
95+ sliderImage . classList . remove ( 'active' ) ;
96+ }
97+ } ) ;
98+ }
99+
61100 </ script >
62101
63102 < style >
You can’t perform that action at this time.
0 commit comments