function backToTop() { let toTopBtn = $(‘#toTopBtn‘); let scrollTop = $(document).scrollTop(); let clientHeight = document.documentElement.clientHeight; toggleToTopBtn(toTopBtn, scrollTop, clientHeight); $(window).on(‘scroll‘, () => { scrollTop = $(document).scrollTop(); toggleToTopBtn(toTopBtn, scrollTop, clientHeight); }) toTopBtn.on(‘click‘, () => { $(‘body,html‘).animate({ scrollTop: 0 }, 500) }) }
function toggleToTopBtn(toTopBtn, scrollTop, clientHeight) { if (scrollTop > clientHeight) { toTopBtn.css(‘visibility‘, ‘visible‘); } else { toTopBtn.css(‘visibility‘, ‘hidden‘); } }