Widget:ScrollToTopSidebar

From Buddha-Nature
Revision as of 13:40, 13 December 2024 by JeremiP (talk | contribs) ((by SublimeText.Mediawiker))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

<script> // Create the scroll to top button const scrollToTopButtonSb = document.createElement('button'); scrollToTopButtonSb.innerHTML = 'Top '; // Text and FA to top icon scrollToTopButtonSb.setAttribute('id', 'scrollToTop');

// Add the button to the document body const container = document.getElementById('scrollToTopContainer'); container.appendChild(scrollToTopButtonSb);

// Show/hide the button based on scroll position window.addEventListener('scroll', () => { if (window.pageYOffset > 300) { scrollToTopButtonSb.style.display = 'block'; } else { scrollToTopButtonSb.style.display = 'none'; } });

// Scroll to top when the button is clicked scrollToTopButtonSb.addEventListener('click', () => { window.scrollTo({ top: 0, behavior: 'smooth' }); }); </script>