diff --git a/src/js/08-scroll-up.js b/src/js/08-scroll-up.js index ba46fb8..062a8ae 100644 --- a/src/js/08-scroll-up.js +++ b/src/js/08-scroll-up.js @@ -4,18 +4,14 @@ goTopBtn.addEventListener('click', goTop); window.addEventListener('scroll', trackScroll); function trackScroll() { - const offset = window.pageYOffset; + const offset = window.scrollY; const coords = document.documentElement.clientHeight; - if (offset > coords) { - goTopBtn.classList.add('scroll__go-top--show'); - } else { - goTopBtn.classList.remove('scroll__go-top--show'); - } + goTopBtn.classList.toggle('scroll__go-top--show', offset > coords); } function goTop() { - if (window.pageYOffset > 0) { + if (window.scrollY > 0) { window.scrollBy(0, -75); - setTimeout(goTop, 0); + requestAnimationFrame(goTop); } }