File tree 1 file changed +23
-8
lines changed
1 file changed +23
-8
lines changed Original file line number Diff line number Diff line change 1
- import React from 'react'
1
+ import React , { useEffect } from 'react'
2
2
import CONFIG_HEXO from '../config_hexo'
3
+ let wrapperTop = 0
3
4
4
5
/**
5
6
* 跳转到评论区
@@ -10,16 +11,30 @@ const JumpToCommentButton = () => {
10
11
if ( ! CONFIG_HEXO . WIDGET_TO_COMMENT ) {
11
12
return < > </ >
12
13
}
13
- function navToComment ( ) {
14
- const commentElement = document . getElementById ( 'comment' )
15
- if ( commentElement ) {
16
- commentElement ?. scrollIntoView ( { behavior : 'smooth' , block : 'start' , inline : 'nearest' } )
17
- }
14
+
15
+ function updateHeaderHeight ( ) {
16
+ setTimeout ( ( ) => {
17
+ if ( window ) {
18
+ const wrapperElement = document . getElementById ( 'comment' )
19
+ wrapperTop = wrapperElement ?. offsetTop
20
+ }
21
+ } , 500 )
22
+ }
23
+ function navToComment ( ) {
24
+ window . scrollTo ( { top : wrapperTop , behavior : 'smooth' } )
25
+ // 兼容性不好
26
+ // const commentElement = document.getElementById('comment')
27
+ // if (commentElement) {
28
+ // commentElement?.scrollIntoView({ behavior: 'smooth', block: 'start', inline: 'nearest' })
18
29
}
19
30
31
+ useEffect ( ( ) => {
32
+ updateHeaderHeight ( )
33
+ } )
34
+
20
35
return ( < div className = 'flex space-x-1 items-center justify-center transform hover:scale-105 duration-200 w-7 h-7 text-center' onClick = { navToComment } >
21
- < i className = 'fas fa-comment text-xs' />
22
- </ div > )
36
+ < i className = 'fas fa-comment text-xs' />
37
+ </ div > )
23
38
}
24
39
25
40
export default JumpToCommentButton
You can’t perform that action at this time.
0 commit comments