Skip to content

Commit 2ef7e15

Browse files
author
mochibot
committed
tutorial wesbos#13 completed
1 parent 775b52b commit 2ef7e15

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

13 - Slide in on Scroll/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Things learned:
2+
3+
- Use of debounce to limit the rate at which a function can fire, to ensure that the function does not fire so often that it bricks browser performance
4+
- How to detect if browser window is scrolled to the position of the image

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

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

4545
<script>
46+
//only run function every 20 ms to reduce the number of events
4647
function debounce(func, wait = 20, immediate = true) {
4748
var timeout;
4849
return function() {
@@ -58,6 +59,24 @@ <h1>Slide in on Scroll</h1>
5859
};
5960
}
6061

62+
let images = document.querySelectorAll('img')
63+
64+
function checkSlide(event) {
65+
images.forEach(item => {
66+
const start = (window.scrollY + window.innerHeight) - item.height / 2;
67+
const end = item.offsetTop + item.height;
68+
if (start > item.offsetTop && window.scrollY < end) {
69+
item.classList.add('active')
70+
} else {
71+
item.classList.remove('active')
72+
}
73+
})
74+
}
75+
76+
document.addEventListener('scroll', debounce(checkSlide))
77+
78+
79+
6180
</script>
6281

6382
<style>

0 commit comments

Comments
 (0)