File tree 3 files changed +45
-1
lines changed
3 files changed +45
-1
lines changed Original file line number Diff line number Diff line change @@ -2,8 +2,9 @@ import type { Metadata } from 'next'
2
2
import { Inter } from 'next/font/google'
3
3
import './globals.css'
4
4
import type { ReactNode } from 'react'
5
- import { BackgroundBeamsWithCollision } from '@/components/ui/background-beams-with-collision '
5
+ import { BackgroundBeamsWithCollision } from '@/components/ui/BeamsWithCollision '
6
6
import { Header } from '@/components/ui/header'
7
+ import { ScrollToTop } from '@/components/ui/ScrollToTop'
7
8
8
9
const inter = Inter ( { subsets : [ 'latin' ] } )
9
10
@@ -26,6 +27,7 @@ export default function RootLayout({ children }: { children: ReactNode }) {
26
27
{ children }
27
28
</ div >
28
29
</ main >
30
+ < ScrollToTop />
29
31
</ body >
30
32
</ html >
31
33
)
File renamed without changes.
Original file line number Diff line number Diff line change
1
+ 'use client'
2
+
3
+ import { useEffect , useState } from 'react'
4
+ import { Button } from './button'
5
+ import { ArrowUp } from 'lucide-react'
6
+ import { cn } from '@/lib/utils'
7
+
8
+ export const ScrollToTop = ( ) => {
9
+ const [ isVisible , setIsVisible ] = useState ( false )
10
+
11
+ const handleScroll = ( ) => {
12
+ const scrollY = window . scrollY
13
+ setIsVisible ( scrollY > 200 )
14
+ }
15
+
16
+ const handleScrollToTop = ( ) => {
17
+ window . scrollTo ( {
18
+ top : 0 ,
19
+ behavior : 'smooth'
20
+ } )
21
+ }
22
+
23
+ useEffect ( ( ) => {
24
+ window . addEventListener ( 'scroll' , handleScroll )
25
+ return ( ) => window . removeEventListener ( 'scroll' , handleScroll )
26
+ } , [ ] )
27
+
28
+ return (
29
+ < Button
30
+ variant = "outline"
31
+ size = "icon"
32
+ className = { cn (
33
+ 'fixed bottom-8 right-8 z-50 rounded-full shadow-lg transition-all duration-300' ,
34
+ isVisible ? 'translate-y-0 opacity-100' : 'translate-y-28 opacity-0'
35
+ ) }
36
+ onClick = { handleScrollToTop }
37
+ aria-label = "返回顶部"
38
+ >
39
+ < ArrowUp className = "h-4 w-4" />
40
+ </ Button >
41
+ )
42
+ }
You can’t perform that action at this time.
0 commit comments