Widget:ScrollToTopSidebar: Difference between revisions
((by SublimeText.Mediawiker)) |
((by SublimeText.Mediawiker)) |
||
| Line 2: | Line 2: | ||
// Create the scroll to top button | // Create the scroll to top button | ||
const scrollToTopButton = document.createElement('button'); | const scrollToTopButton = document.createElement('button'); | ||
scrollToTopButton.innerHTML = 'Top <i class="fa-sharp-duotone fa-solid fa-up-to-bracket"></i>'; // | scrollToTopButton.innerHTML = 'Top <i class="fa-sharp-duotone fa-solid fa-up-to-bracket"></i>'; // Text and FA to top icon | ||
scrollToTopButton.setAttribute('id', 'scrollToTop'); | scrollToTopButton.setAttribute('id', 'scrollToTop'); | ||
scrollToTopButton.style.cssText = ` | scrollToTopButton.style.cssText = ` | ||
Revision as of 17:10, 1 November 2024
<script> // Create the scroll to top button const scrollToTopButton = document.createElement('button'); scrollToTopButton.innerHTML = 'Top '; // Text and FA to top icon scrollToTopButton.setAttribute('id', 'scrollToTop'); scrollToTopButton.style.cssText = ` position: fixed; right: 20px; bottom: 20px; width: 50px; height: 50px; border-radius: 50%; background-color: #333; color: #fff; font-size: 24px; border: none; cursor: pointer; display: none; z-index: 1000; `;
// Add the button to the document body document.body.appendChild(scrollToTopButton);
// Show/hide the button based on scroll position window.addEventListener('scroll', () => { if (window.pageYOffset > 300) { scrollToTopButton.style.display = 'block'; } else { scrollToTopButton.style.display = 'none'; } });
// Scroll to top when the button is clicked scrollToTopButton.addEventListener('click', () => { window.scrollTo({ top: 0, behavior: 'smooth' }); }); </script>