Skip to content

Commit b69e175

Browse files
committed
Finished wesbos#13
1 parent 1464b18 commit b69e175

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ <h1>Slide in on Scroll</h1>
4343
</div>
4444

4545
<script>
46+
// debounce is used to limit the frequency
47+
// with which the contained function runs.
4648
function debounce(func, wait = 20, immediate = true) {
4749
var timeout;
4850
return function() {
@@ -58,6 +60,32 @@ <h1>Slide in on Scroll</h1>
5860
};
5961
}
6062

63+
const sliderImages = document.querySelectorAll('.slide-in');
64+
65+
function checkslide(e) {
66+
// console.count(e);
67+
68+
sliderImages.forEach(slideImage => {
69+
// 1/3 of the way down the image
70+
const slideInAt = ((window.scrollY + window.innerHeight) - (slideImage.height / 3));
71+
72+
// bottom of the image
73+
const imageBottom = (slideImage.offsetTop + slideImage.height);
74+
75+
// comparison vars
76+
const isHalfShown = slideInAt > slideImage.offsetTop;
77+
const isNotScrolledPast = window.scrollY < imageBottom;
78+
79+
// comparison & add/remove class
80+
if (isHalfShown && isNotScrolledPast) {
81+
slideImage.classList.add('active');
82+
} else {
83+
slideImage.classList.remove('active');
84+
}
85+
});
86+
}
87+
window.addEventListener('scroll', debounce(checkslide));
88+
6189
</script>
6290

6391
<style>

0 commit comments

Comments
 (0)