豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit d42900e

Browse files
authored
feat: add scroll to top button (#326)
1 parent 5e0ca78 commit d42900e

File tree

4 files changed

+145
-35
lines changed

4 files changed

+145
-35
lines changed

package-lock.json

Lines changed: 104 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"dependencies": {
1313
"@fortawesome/fontawesome-svg-core": "^6.4.2",
1414
"@fortawesome/free-brands-svg-icons": "^6.4.2",
15+
"@fortawesome/free-solid-svg-icons": "^6.5.1",
1516
"@fortawesome/react-fontawesome": "^0.2.0",
1617
"@headlessui/react": "^1.7.17",
1718
"@headlessui/tailwindcss": "^0.2.0",

src/app/page.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { PrimaryFeatures } from '@/components/PrimaryFeatures'
88
import { SecondaryFeatures } from '@/components/SecondaryFeatures'
99
import { Testimonials } from '@/components/Testimonials'
1010
import { Founders } from '@/components/Founders'
11+
import { ScrollToTop } from '@/components/ScrollToTop'
1112

1213
export default function Home() {
1314
return (
@@ -24,6 +25,7 @@ export default function Home() {
2425
<Faqs />
2526
</main>
2627
<Footer />
28+
<ScrollToTop/>
2729
</>
2830
)
2931
}

src/components/ScrollToTop.jsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use client';
2+
import { Button } from "./Button";
3+
import {useState, useEffect} from "react"
4+
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
5+
import { faArrowUp } from "@fortawesome/free-solid-svg-icons";
6+
export function ScrollToTop(){
7+
const [isVisible, setIsVisible] = useState(false)
8+
9+
const toggleVisibility = ()=> {
10+
if (window.scrollY > 300){
11+
setIsVisible(true)
12+
}
13+
else{
14+
setIsVisible(false)
15+
}
16+
}
17+
const scrollToTop = () =>{
18+
window.scrollTo({
19+
top:0,
20+
behavior:"smooth",
21+
})
22+
}
23+
24+
useEffect(() => {
25+
window.addEventListener("scroll", toggleVisibility)
26+
return () => {
27+
window.removeEventListener("scroll", toggleVisibility)
28+
}
29+
}, [])
30+
return(
31+
<div className="fixed bottom-2 right-2">
32+
<Button className={isVisible ? 'opacity-100' : 'opacity-0'} variant={"solid"} onClick={() => scrollToTop()}>
33+
<FontAwesomeIcon icon={faArrowUp} color="white" size="xl" className="h-6 w-6" aria-hidden="true"/>
34+
</Button>
35+
</div>
36+
)
37+
38+
}

0 commit comments

Comments
 (0)