Skip to content

Commit bdd0ab4

Browse files
committed
adding script wesbos#13
1 parent 0842d99 commit bdd0ab4

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

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

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

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

6387
<style>

0 commit comments

Comments
 (0)