回到顶部按钮

一个最小且响应迅速的“Back to Top”按钮组件,支持深色模式。它在向下滚动后显示,并使用平滑滚动在单击时将用户返回到页面顶部。没有 JavaScript 用于滚动,只有 CSS。

预览

HTML 代码

<div class="fixed bottom-4 right-4">
  <a href="#" class="bg-blue-500 text-white p-3 rounded-full shadow-md transition-opacity duration-300 opacity-0 group-hover:opacity-100 dark:bg-blue-700 dark:text-gray-200">
    <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 10l7-7m0 0l7 7m-7-7v18"></path></svg>
  </a>
</div>

<style>
  /* This simple example uses CSS for smooth scrolling. For a production site, you might use a small amount of JavaScript for better compatibility and control over the scroll behavior and when the button appears. */
  html {
    scroll-behavior: smooth;
  }

  /* Basic example to show/hide the button based on scroll position. 
     A robust solution would involve JavaScript Intersection Observer or scroll events. */
  body:before {
    content: "";
    height: 200vh; /* Simulate a long page */
    display: block;
  }

  .group-hover\:opacity-100:hover + .fixed a {
      opacity: 1; /* This part is flawed for demonstrating scroll-based visibility without JS */
  }

  /* A better approach for scroll-based visibility would require JavaScript */
  .fixed a {
      /* Initially hidden, JS would make it visible on scroll */
      opacity: 0;
      transition: opacity 0.3s ease-in-out;
  }


</style>

相关组件

回到顶部按钮

一个响应式返回顶部按钮组件,采用玻璃拟态效果,适用于专业的企业网站。它具有简单的布局和最少的元素,适用于浅色和深色主题。

打开

粗野主义返回顶部按钮

一个在Tailwind CSS中具有高对比度、大胆排版和锐利边缘的粗犷风格返回顶部按钮。包括响应式大小和暗模式支持。

打开

返回顶部按钮

返回顶部按钮组件,采用3D设计、鲜艳的色调和复杂的交互元素,适用于社交媒体界面,使用Tailwind CSS。包括响应式设计和暗主题支持.

打开