1
- import { NextPage } from ' next'
2
- import Image from ' next/image'
3
- import styles from ' ./NavBar.module.scss'
4
- import { useContext , useEffect , useState } from ' react'
5
- import { ThemeContext } from ' @/stores/theme'
6
- import { Themes } from ' @/constants/enum'
7
- import { usePathname } from ' next/navigation'
1
+ import { NextPage } from " next"
2
+ import Image from " next/image"
3
+ import styles from " ./NavBar.module.scss"
4
+ import { useContext , useEffect , useState } from " react"
5
+ import { ThemeContext } from " @/stores/theme"
6
+ import { Themes } from " @/constants/enum"
7
+ import { usePathname } from " next/navigation"
8
8
9
9
export interface INavBarItemProps {
10
10
id : number
@@ -17,9 +17,9 @@ export interface INavBarProps {
17
17
}
18
18
19
19
const NavBar : NextPage < INavBarProps > = ( { NavData } ) => {
20
-
20
+ const [ IsHide , setIsHide ] = useState < boolean > ( false )
21
21
const { theme, setTheme } = useContext ( ThemeContext )
22
- const [ searchQuery , setSearchQuery ] = useState < string > ( '' )
22
+ const [ searchQuery , setSearchQuery ] = useState < string > ( "" )
23
23
const [ IsFocus , setIsFocus ] = useState < boolean > ( false )
24
24
const [ IsActive , setIsActive ] = useState < boolean > ( false )
25
25
const pathname = usePathname ( )
@@ -30,26 +30,37 @@ const NavBar: NextPage<INavBarProps> = ({ NavData }) => {
30
30
const handleSearchInput = ( e : React . ChangeEvent < HTMLInputElement > ) : void => {
31
31
setSearchQuery ( e . target . value )
32
32
}
33
- const handleClick = ( e :MouseEvent ) => {
34
- if ( ! e . target ) return ;
33
+ const handleClick = ( e : MouseEvent ) => {
34
+ if ( ! e . target ) return
35
35
const target = e . target as HTMLElement
36
- if ( target . classList . value . indexOf ( ' more' ) === - 1 ) {
36
+ if ( target . classList . value . indexOf ( " more" ) === - 1 ) {
37
37
setIsActive ( false )
38
38
}
39
-
40
- }
41
- useEffect ( ( ) => {
42
- document . addEventListener ( 'click' , handleClick )
43
- return ( ) => {
44
- document . removeEventListener ( 'click' , handleClick )
45
39
}
46
- } )
40
+ useEffect ( ( ) => {
41
+ document . addEventListener ( "scroll" , handlerScroll )
42
+ document . addEventListener ( "click" , handleClick )
47
43
44
+ return ( ) => {
45
+ document . removeEventListener ( "click" , handleClick )
46
+ document . addEventListener ( "scroll" , handlerScroll )
47
+ }
48
+ } )
49
+ // 滚动事件处理函数
50
+ const handlerScroll = ( ) => {
51
+ const scrollTop =
52
+ document . documentElement . scrollTop || document . body . scrollTop
53
+ if ( scrollTop > 446 ) {
54
+ setIsHide ( true )
55
+ } else {
56
+ setIsHide ( false )
57
+ }
58
+ }
48
59
49
60
//监听点击除了create__btn以外的地方
50
-
61
+
51
62
return (
52
- < nav className = { styles . nav } >
63
+ < nav className = { ` ${ styles . nav } ${ IsHide ? styles . hide : '' } ` } >
53
64
< div className = { styles . nav_wrapper } >
54
65
< div className = { styles . nav__left } >
55
66
< div className = { styles . nav__logo } >
@@ -62,7 +73,7 @@ useEffect(() => {
62
73
< li
63
74
key = { item . id }
64
75
className = { `${ styles . nav__menu__item } ${
65
- pathname === item . url ? styles . active : ''
76
+ pathname === item . url ? styles . active : ""
66
77
} `}
67
78
>
68
79
< a href = { item . url } > { item . name } </ a >
@@ -77,16 +88,16 @@ useEffect(() => {
77
88
</ div >
78
89
< div className = { styles . nav__right } >
79
90
< div
80
- className = { `${ styles . nav__search } ${ IsFocus ? styles . focus : '' } ` }
91
+ className = { `${ styles . nav__search } ${ IsFocus ? styles . focus : "" } ` }
81
92
>
82
93
< input
83
94
type = "text"
84
- placeholder = { IsFocus ? ' 文章、专栏、用户' : '搜索' }
95
+ placeholder = { IsFocus ? " 文章、专栏、用户" : "搜索" }
85
96
className = { styles . nav__search__input }
86
97
onChange = { handleSearchInput }
87
98
onFocus = { ( ) => setIsFocus ( true ) }
88
99
onBlur = { ( ) => {
89
- if ( searchQuery === '' ) {
100
+ if ( searchQuery === "" ) {
90
101
setIsFocus ( false )
91
102
}
92
103
} }
@@ -99,15 +110,18 @@ useEffect(() => {
99
110
</ div >
100
111
< div className = { styles . create__group } >
101
112
< div
102
- className = { `${ styles . create__btn } ${ IsFocus ? styles . focus : '' } ` }
113
+ className = { `${ styles . create__btn } ${ IsFocus ? styles . focus : "" } ` }
103
114
>
104
115
< button className = { styles . add__btn } > 创作者中心</ button >
105
- < i className = { styles . more } onClick = { ( ) => {
106
- setIsActive ( ! IsActive )
107
- } } >
116
+ < i
117
+ className = { styles . more }
118
+ onClick = { ( ) => {
119
+ setIsActive ( ! IsActive )
120
+ } }
121
+ >
108
122
< svg
109
123
className = { `${ styles . more__icon } ${
110
- IsActive ? styles . active : ''
124
+ IsActive ? styles . active : ""
111
125
} `}
112
126
width = "12"
113
127
height = "12"
@@ -116,15 +130,15 @@ useEffect(() => {
116
130
xmlns = "http://www.w3.org/2000/svg"
117
131
>
118
132
< path
119
- className = ' more__icon__path'
133
+ className = " more__icon__path"
120
134
d = "M2.45025 4.82383C2.17422 4.49908 2.40501 4 2.83122 4H9.16878C9.59499 4 9.82578 4.49908 9.54975 4.82382L6.38097 8.5518C6.1813 8.7867 5.8187 8.7867 5.61903 8.5518L2.45025 4.82383Z"
121
135
fill = "white"
122
136
> </ path >
123
137
</ svg >
124
138
</ i >
125
139
< ul
126
140
className = { `${ styles . more__list } ${
127
- IsActive ? styles . active : ''
141
+ IsActive ? styles . active : ""
128
142
} `}
129
143
>
130
144
< li className = { styles . more__item } >
@@ -152,7 +166,7 @@ useEffect(() => {
152
166
< div className = { styles . theme__wrapper } >
153
167
< i
154
168
className = { `${ styles . theme__icon } ${
155
- theme === Themes . light ? ' fas fa-moon' : ' fas fa-sun'
169
+ theme === Themes . light ? " fas fa-moon" : " fas fa-sun"
156
170
} `}
157
171
onClick = { ( ) : void => {
158
172
if ( theme === Themes . light ) {
0 commit comments