Skip to content

Commit 9c1cbf7

Browse files
author
Kyle Bradshaw
committed
13 slide in on scroll, final kb
1 parent 91d8300 commit 9c1cbf7

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

13 - Slide in on Scroll/index-START.html renamed to 13 - Slide in on Scroll/index.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+
const sliderImages = document.querySelectorAll('.slide-in');
62+
63+
function checkSlide(e) {
64+
// console.log(e);
65+
sliderImages.forEach(image => {
66+
//when img is 50% visible, add active class;
67+
// console.log(window.scrollY,window.scrollX,image.offset);
68+
const slideInAt = (window.scrollY + window.innerHeight) - (image.height/2); //pixel level when each img should be slide in at
69+
70+
//scroll past the image you want to slide back out
71+
const imageBottom = image.offsetTop + image.height; //top of image is far from top of doc + height so you know when it is offscreen
72+
73+
const isHalfShown = slideInAt > image.offsetTop;
74+
const isNotScrolledPast = window.scrollY < imageBottom;
75+
76+
console.log(window.scrollY, slideInAt, imageBottom, isHalfShown, isNotScrolledPast);
77+
if(isHalfShown && isNotScrolledPast) {
78+
image.classList.add('active');
79+
} else {
80+
image.classList.remove('active');
81+
}
82+
83+
})
84+
}
85+
86+
window.addEventListener('scroll', debounce(checkSlide));
87+
6188
</script>
6289

6390
<style>

0 commit comments

Comments
 (0)