출처: http://ezcode.kr/study/view/209
사진 출처: https://stackoverflow.com/questions/21064101/understanding-offsetwidth-clientwidth-scrollwidth-and-height-respectively
출처: https://brooknovak.wordpress.com/2013/06/11/find-the-character-position-using-javascript-fast-big-pages-all-browsers-no-preprocessing/
<script>
function debounce(func, wait = 20, immediate = true) {
var timeout;
return function () {
var context = this,
args = arguments;
var later = function () {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
}
const imgs = document.querySelectorAll('.slide-in');
function imgSlide() {
imgs.forEach((img) => {
// console.log(window.scrollY);
// console.log(window.innerHeight);
const slideInAt =
window.scrollY + window.innerHeight - img.height / 2;
const imgBottom = img.offsetTop + img.height;
const isHalfshown = slideInAt > img.offsetTop;
const isNotPassed = window.scrollY < imgBottom;
if (isHalfshown && isNotPassed) {
img.classList.add('active');
} else {
img.classList.remove('active');
}
});
}
window.addEventListener('scroll', debounce(imgSlide));
</script>