function toggleToTopBtn(toTopBtn, scrollTop, clientHeight) { if (scrollTop > clientHeight) { toTopBtn.css(‘display‘, ‘block‘) } else { toTopBtn.css(‘display‘, ‘none‘) } } $(document).ready(function () { /*回到顶部*/ 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) }) })