Skip to content

Commit d7622c8

Browse files
committed
Complete
1 parent 2bfeaa2 commit d7622c8

File tree

2 files changed

+24
-140
lines changed

2 files changed

+24
-140
lines changed

13 - Slide in on Scroll/index-FINISHED.html

Lines changed: 0 additions & 140 deletions
This file was deleted.

13 - Slide in on Scroll/index-START.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ <h1>Slide in on Scroll</h1>
4343
</div>
4444

4545
<script>
46+
// debounce makes it so checkSlide function will only run so many times. This is done for performance reasons because the scroll event will run over and over every time you scroll.
4647
function debounce(func, wait = 20, immediate = true) {
4748
var timeout;
4849
return function() {
@@ -58,6 +59,29 @@ <h1>Slide in on Scroll</h1>
5859
};
5960
}
6061

62+
const sliderImages = document.querySelectorAll('.slide-in');
63+
64+
function checkSlide (e) {
65+
// Gets the half way point in the image.
66+
sliderImages.forEach(sliderImage =>{
67+
const slideInAt = (window.scrollY + window.innerHeight) - sliderImage.height / 2;
68+
69+
//Repeat the animation when scrolling back up.
70+
const imageBottom = sliderImage.offsetTop + sliderImage.height;
71+
72+
const isHalfShown = slideInAt > sliderImage.offsetTop;
73+
// Makes sure we are not scrolled passed the bottom.
74+
const isNotScrolledPast = window.scrollY < imageBottom;
75+
if (isHalfShown && isNotScrolledPast) {
76+
sliderImage.classList.add('active');
77+
}else{
78+
sliderImage.classList.remove('active');
79+
}
80+
})
81+
}
82+
83+
window.addEventListener('scroll', debounce(checkSlide));
84+
6185
</script>
6286

6387
<style>

0 commit comments

Comments
 (0)