Skip to content

Commit 7a0d249

Browse files
committed
Slide in Image assignment.
1 parent 4cf1b17 commit 7a0d249

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

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

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

61+
//image is halfscrolled it should be active
62+
//image is scrolled past it should be hidden
63+
64+
65+
/*
66+
how do you get if image is half scrolled
67+
* check for the image offsetTop and calculate if the scroll postion is past that offsetTop
68+
* scroll position is calculated by checking the ScrollY position of the window + the window height
69+
* and the image half position
70+
71+
*/
72+
73+
/*
74+
how do you get if image is scrolled past
75+
* get the image bottom position
76+
* check if the window scrollY is past that image bottom position
77+
78+
*/
79+
80+
81+
const sliderImages = document.querySelectorAll('.slide-in');
82+
window.addEventListener('scroll', debounce(checkSlide));
83+
84+
function checkSlide(e) {
85+
sliderImages.forEach(sliderImage => {
86+
const slideInAt = (window.scrollY + window.innerHeight) - (sliderImage.height/2);
87+
const isHalfScrolled = slideInAt > sliderImage.offsetTop;
88+
89+
const imageBottom = sliderImage.offsetTop + sliderImage.height;
90+
const isScrolledPast = window.scrollY > imageBottom;
91+
if(isHalfScrolled && !isScrolledPast) {
92+
sliderImage.classList.add('active');
93+
}
94+
else {
95+
sliderImage.classList.remove('active');
96+
}
97+
});
98+
}
99+
61100
</script>
62101

63102
<style>

0 commit comments

Comments
 (0)