-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
218851c
commit 47a1b6b
Showing
7 changed files
with
73 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"use client"; | ||
import React, { useState, useCallback, useEffect } from "react"; | ||
import { debounce } from "lodash-es"; | ||
|
||
export default function ScrollToTop() { | ||
const [showScroll, setShowScroll] = useState(false); | ||
|
||
const handleScroll = useCallback( | ||
() => setShowScroll(window.scrollY > 500), | ||
[] | ||
); | ||
|
||
useEffect(() => { | ||
window.addEventListener("scroll", handleScroll); | ||
setShowScroll(false); | ||
return () => window.removeEventListener("scroll", handleScroll); | ||
}, [handleScroll]); | ||
|
||
const scrollToTop = useCallback(() => { | ||
window.scrollTo({ top: 0, behavior: "smooth" }); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
{showScroll && ( | ||
<button | ||
onClick={scrollToTop} | ||
className="fixed bottom-4 right-4 p-2 bg-gray-800 text-white rounded-full shadow-lg transition-opacity duration-300" // 添加过渡效果 | ||
style={{ visibility: showScroll ? "visible" : "hidden" }} // 使用visibility控制显示 | ||
> | ||
回到顶部 | ||
</button> | ||
)} | ||
</> | ||
); | ||
} |