From cf769f5548a73c4380db1e96d79d959ef077ca3a Mon Sep 17 00:00:00 2001 From: jzz Date: Sat, 7 Dec 2024 22:07:59 +0800 Subject: [PATCH 1/2] Adding a Scroll-To-top Button --- src/components/Layout/Toc.tsx | 164 +++++++++++++++++++++++++--------- 1 file changed, 120 insertions(+), 44 deletions(-) diff --git a/src/components/Layout/Toc.tsx b/src/components/Layout/Toc.tsx index 5308c602ce5..52434696bb3 100644 --- a/src/components/Layout/Toc.tsx +++ b/src/components/Layout/Toc.tsx @@ -6,58 +6,134 @@ import cx from 'classnames'; import {useTocHighlight} from './useTocHighlight'; import type {Toc} from '../MDX/TocContext'; +import {useState, useEffect} from 'react'; + +function ScrollToTop() { + const [isVisible, setIsVisible] = useState(false); + + // 控制按钮显示 + const toggleVisibility = () => { + if (window.pageYOffset > 300) { + setIsVisible(true); + } else { + setIsVisible(false); + } + }; + + // 滚动到顶部 + const scrollToTop = () => { + window.scrollTo({ + top: 0, + behavior: 'smooth', + }); + }; + + useEffect(() => { + window.addEventListener('scroll', toggleVisibility); + return () => { + window.removeEventListener('scroll', toggleVisibility); + }; + }, []); + + return ( + <> + {isVisible && ( +
+ + + +
+ )} + + ); +} + export function Toc({headings}: {headings: Toc}) { const {currentIndex} = useTocHighlight(); // TODO: We currently have a mismatch between the headings in the document // and the headings we find in MarkdownPage (i.e. we don't find Recap or Challenges). // Select the max TOC item we have here for now, but remove this after the fix. const selectedIndex = Math.min(currentIndex, headings.length - 1); + return ( - + + ); } From e6cd277a3365a8397d53f6ae6accb3fdeea57e92 Mon Sep 17 00:00:00 2001 From: jzz Date: Sat, 7 Dec 2024 22:18:21 +0800 Subject: [PATCH 2/2] Adding a Scroll-To-top Button --- src/components/Layout/Toc.tsx | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/src/components/Layout/Toc.tsx b/src/components/Layout/Toc.tsx index 52434696bb3..aa6dcba9dda 100644 --- a/src/components/Layout/Toc.tsx +++ b/src/components/Layout/Toc.tsx @@ -40,27 +40,7 @@ function ScrollToTop() { {isVisible && (
+ className="fixed bottom-10 right-10 cursor-pointer bg-blue-500 rounded-full w-10 h-10 flex justify-center items-center shadow-lg transition-all duration-300 opacity-80 hover:opacity-100 hover:-translate-y-0.5 z-50">