返回顶部按钮

一个采用材料设计的“返回顶部”按钮,在用户向下滚动页面时出现。它支持深色模式和流畅的滚动效果。

预览

HTML 代码

<template>
  <button
    id="back-to-top-btn"
    class="fixed bottom-6 right-6 bg-blue-600 text-white p-3 rounded-full shadow-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-opacity-50 transition duration-300 ease-in-out dark:bg-blue-700 dark:hover:bg-blue-800"
    onclick="scrollToTop()"
    aria-label="Back to top"
  >
    <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="M12 6V4m0 2l-3 3m3-3l3 3m-6 6h6a2 2 0 010 4H6a2 2 0 010-4z"
      ></path>
    </svg>
  </button>
</template>

<script>
  // This script is for demonstration purposes and would typically be in a separate JS file
  const backToTopButton = document.getElementById('back-to-top-btn');

  window.onscroll = function() {
    if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
      backToTopButton.style.display = "block";
    } else {
      backToTopButton.style.display = "none";
    }
  };

  function scrollToTop() {
    window.scrollTo({ top: 0, behavior: 'smooth' });
  }
</script>

<style>
  /* Add any necessary component-specific styles here */
  #back-to-top-btn {
    display: none; /* Hidden by default */
  }
</style>

相关组件

返回顶部按钮组件

一个响应式“返回顶部”按钮,遵循材料设计原则,支持暗黑模式,使用Tailwind CSS进行样式和响应效果.

打开

粗犷风格 返回顶部

一个采用灰阶颜色和Tailwind CSS的野兽派回到顶部按钮组件。它是响应式的,并支持深色模式。

打开

返回顶部按钮组件

一个响应式返回顶部按钮,设计有微交互和鲜艳的色彩,适用于支持深色模式的仪表板界面。

打开