Elegant 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>Elegant Scroll-to-Top Button</title> <style> body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; margin: 0; padding: 0; min-height: 2000px; background: #f8f9fa; } .scroll-to-top { position: fixed; bottom: 40px; right: 40px; width: 50px; height: 50px; background-color: #4CAF50; 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: #45a049; 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); } } </style> </head> <body> <div class="scroll-to-top" 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.style.opacity = '1'; scrollToTopBtn.style.visibility = 'visible'; } else { scrollToTopBtn.style.opacity = '0'; scrollToTopBtn.style.visibility = 'hidden'; } } function scrollToTop() { rootElement.scrollTo({ top: 0, behavior: "smooth" }); } scrollToTopBtn.addEventListener('click', scrollToTop); document.addEventListener('scroll', handleScroll); </script> </body> </html>