Skip to content

Commit 156ee6d

Browse files
author
Alexander Besse
committed
wesbos#13 Completed.
1 parent a35414f commit 156ee6d

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

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

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

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

6384
<style>

0 commit comments

Comments
 (0)