학습일자
2020.10.20
학습내용
- 'debounce' function
: 불필요하게 너무 많이 event에 의한 callback function이 호출되는 경우, 적정 수준으로 조절하기 위한 함수이고, 대상callback function을 괄호로 감싸 사용
- window.scrollY
: 읽기 전용 속성, 문서가 수직으로 얼마나 스크롤됐는지 픽셀 단위로 반환/ 최신 브라우저에서는 값의 정밀도가 픽셀보다 작으므로 반드시 정숫값을 반환하는건 아님
Why window.scrollY + element.getBoundingClientRect().top is not equal to element.offsetTop?
I expected element.getBoundingClientRect(), window.scrollY and element.offsetTop to work as shown on the picture below. But as you can see, the numbers do not add up (in my case, pos.offsetTop - (el.
stackoverflow.com
- 윈도우 창크기
: innerheight, innerwidth
[javascript] 윈도우 창 크기 (window.outerWidth, window.outerHeight, window.outerHeight, window.innerWidth, innerHeight)
자바스크립트에서 윈도우 창 크기에 대해 알아보겠습니다. 익스플로러, 크롬, 파이어폭스, 오페라 모두 같은 값을 출력하는 윈도우 창크기는 window.outerWidth, window.outerHeight, window.outerHeight, window...
sometimes-n.tistory.com
알게된 점
- debounce function은 callback fucntion을 실행하기 위한 scroll 이벤트처럼 불필요하게 과도한 많은 이벤트 발생 시, 적정 수준으로 제어하기 위한 목적으로 활용
- 윈도우 창 크기에 대한 속성을 정확히 알아야 관련 효과 및 배치를 자유롭게 할 수 있음
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 sliderImages = document.querySelectorAll('.slide-in');
function checkSlide() {
sliderImages.forEach(sliderImage => {
// half way through the image
const slideInAt = (window.scrollY + window.innerHeight) - sliderImage.height / 2;
// bottom of the image
const imageBottom = sliderImage.offsetTop + sliderImage.height;
const isHalfShown = slideInAt > sliderImage.offsetTop;
const isNotScrolledPast = window.scrollY < imageBottom;
if (isHalfShown && isNotScrolledPast) {
sliderImage.classList.add('active');
} else {
sliderImage.classList.remove('active');
}
});
}
window.addEventListener('scroll', debounce(checkSlide));
'언어별 학습 자료 > Javascript' 카테고리의 다른 글
"# JavaScript 30" 강의 - 18. Adding Up Times with Reduce (0) | 2020.10.22 |
---|---|
"# JavaScript 30" 강의 - 14. JavaScript References VS Copying (0) | 2020.10.21 |
"# JavaScript 30" 강의 - 12. Key Sequence Detection (0) | 2020.10.20 |
"# JavaScript 30" 강의 - 11. Custom Video Player (0) | 2020.10.16 |
"# JavaScript 30" 강의 - 10. Hold Shift and Check Checkboxes (0) | 2020.10.08 |