File tree Expand file tree Collapse file tree 2 files changed +37
-35
lines changed Expand file tree Collapse file tree 2 files changed +37
-35
lines changed Original file line number Diff line number Diff line change @@ -43,41 +43,6 @@ <h1>Slide in on Scroll</h1>
4343 </ div >
4444
4545 < script src ="scroll.js ">
46- function debounce ( func , wait = 20 , immediate = true ) {
47- var timeout ;
48- return function ( ) {
49- var context = this , args = arguments ;
50- var later = function ( ) {
51- timeout = null ;
52- if ( ! immediate ) func . apply ( context , args ) ;
53- } ;
54- var callNow = immediate && ! timeout ;
55- clearTimeout ( timeout ) ;
56- timeout = setTimeout ( later , wait ) ;
57- if ( callNow ) func . apply ( context , args ) ;
58- } ;
59- }
60-
61- const sliderImages = document . querySelectorAll ( '.slide-in' ) ;
62-
63- function checkSlide ( e ) {
64- sliderImages . forEach ( sliderImage => {
65- // half way through the image
66- const slideInAt = ( window . scrollY + window . innerHeight ) - sliderImage . height / 2 ;
67- // bottom of the image
68- const imageBottom = sliderImage . offsetTop + sliderImage . height ;
69- const isHalfShown = slideInAt > sliderImage . offsetTop ;
70- const isNotScrolledPast = window . scrollY < imageBottom ;
71- if ( isHalfShown && isNotScrolledPast ) {
72- sliderImage . classList . add ( 'active' ) ;
73- } else {
74- sliderImage . classList . remove ( 'active' ) ;
75- }
76- } ) ;
77- }
78-
79- window . addEventListener ( 'scroll' , debounce ( checkSlide ) ) ;
80-
8146 </ script >
8247
8348 < style >
Original file line number Diff line number Diff line change 1+ function debounce ( func , wait = 20 , immediate = true ) {
2+ var timeout ;
3+ return function ( ) {
4+ var context = this , args = arguments ;
5+ var later = function ( ) {
6+ timeout = null ;
7+ if ( ! immediate ) func . apply ( context , args ) ;
8+ } ;
9+ var callNow = immediate && ! timeout ;
10+ clearTimeout ( timeout ) ;
11+ timeout = setTimeout ( later , wait ) ;
12+ if ( callNow ) func . apply ( context , args ) ;
13+ } ;
14+ }
15+
16+ const sliderImages = document . querySelectorAll ( '.slide-in' ) ;
17+
18+ function checkSlide ( e ) {
19+ sliderImages . forEach ( sliderImage => {
20+
21+ //halfway though the image
22+ const slideInAt = ( window . scrollY + window . innerHeight ) - sliderImage . height / 2 ;
23+ //bottom of the image
24+ const imageBottom = sliderImage . offsetTop + sliderImage . height ;
25+
26+ const isHalfShown = slideInAt > sliderImage . offsetTop ;
27+ const isNotScrolledPast = window . scrollY < imageBottom ;
28+
29+ if ( isHalfShown && isNotScrolledPast ) {
30+ sliderImage . classList . add ( 'active' ) ;
31+ } else {
32+ sliderImage . classList . remove ( 'active' ) ;
33+ }
34+ } )
35+ }
36+
37+ window . addEventListener ( 'scroll' , debounce ( checkSlide ) ) ;
You can’t perform that action at this time.
0 commit comments