Skip to content

Commit 6924caa

Browse files
committed
thirteen complete
1 parent 6cd9b22 commit 6924caa

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,33 @@ <h1>Slide in on Scroll</h1>
5858
};
5959
}
6060

61+
62+
const sliderImages = document.querySelectorAll('.slide-in');
63+
64+
function checkSlide(e) {
65+
console.count(e);
66+
sliderImages.forEach(sliderImage => {
67+
const slideInAt = (window.scrollY + window.innerHeight) - sliderImage.height / 2;
68+
//window.scrollY calculates from the top of the window
69+
//we want from the bottom of the window
70+
//pixel level where the image should be scrolled in
71+
72+
const imageBottom = sliderImage.offsetTop + sliderImage.height
73+
//offsetTop - top of this image is this far from top of window
74+
//add height of image to get the bottom of the img
75+
76+
const isHalfShown = slideInAt > sliderImage.offsetTop;
77+
const isNotScrolledPast = window.scrollY < imageBottom;
78+
if (isHalfShown && isNotScrolledPast){
79+
sliderImage.classList.add('active');
80+
} else {
81+
sliderImage.classList.remove('active');
82+
}
83+
});
84+
}
85+
86+
window.addEventListener('scroll', debounce(checkSlide));
87+
6188
</script>
6289

6390
<style>

0 commit comments

Comments
 (0)