Scroll To Top Button - MeggiTools
Run
Toggle Theme
Share Link
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Scroll to Top Button</title> <style> body { font-family: Arial, sans-serif; margin: 0; padding: 0; min-height: 2000px; background: linear-gradient(45deg, #f3ec78, #af4261); } .scroll-to-top { position: fixed; bottom: 40px; right: 40px; width: 50px; height: 50px; background-color: #ff5722; border-radius: 50%; display: flex; justify-content: center; align-items: center; font-size: 24px; color: #fff; cursor: pointer; box-shadow: 0 6px 16px rgba(0, 0, 0, 0.3); transition: all 0.3s ease-in-out; z-index: 1000; } .scroll-to-top:hover { background-color: #e64a19; transform: translateY(-10px); box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4); } .scroll-to-top:active { transform: translateY(-5px); box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2); } .scroll-to-top-icon { animation: bounce 2s infinite; } @keyframes bounce { 0%, 20%, 50%, 80%, 100% { transform: translateY(0); } 40% { transform: translateY(-10px); } 60% { transform: translateY(-5px); } } .scroll-to-top-fade { opacity: 0; visibility: hidden; } .scroll-to-top-show { opacity: 1; visibility: visible; } .scroll-to-top.fade-in { animation: fadeIn 0.5s; } .scroll-to-top.fade-out { animation: fadeOut 0.5s; } @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } } @keyframes fadeOut { from { opacity: 1; } to { opacity: 0; } } </style> </head> <body> <div class="scroll-to-top scroll-to-top-fade" id="scrollToTopBtn"> <span class="scroll-to-top-icon">⬆️</span> </div> <script> const scrollToTopBtn = document.getElementById('scrollToTopBtn'); const rootElement = document.documentElement; function handleScroll() { const scrollTotal = rootElement.scrollHeight - rootElement.clientHeight; if ((rootElement.scrollTop / scrollTotal) > 0.20) { scrollToTopBtn.classList.remove('scroll-to-top-fade'); scrollToTopBtn.classList.add('scroll-to-top-show', 'fade-in'); } else { scrollToTopBtn.classList.remove('scroll-to-top-show', 'fade-in'); scrollToTopBtn.classList.add('scroll-to-top-fade', 'fade-out'); } } function scrollToTop() { rootElement.scrollTo({ top: 0, behavior: "smooth" }); } scrollToTopBtn.addEventListener('click', scrollToTop); document.addEventListener('scroll', handleScroll); </script> </body> </html>