Skip to content

Commit 37c0b07

Browse files
authored
Merge pull request tangly1024#156 from tangly1024/bug-scroll-comment-button
bug-滚动到评论区
2 parents 3a47449 + 391286c commit 37c0b07

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed
+23-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import React from 'react'
1+
import React, { useEffect } from 'react'
22
import CONFIG_HEXO from '../config_hexo'
3+
let wrapperTop = 0
34

45
/**
56
* 跳转到评论区
@@ -10,16 +11,30 @@ const JumpToCommentButton = () => {
1011
if (!CONFIG_HEXO.WIDGET_TO_COMMENT) {
1112
return <></>
1213
}
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' })
1829
}
1930

31+
useEffect(() => {
32+
updateHeaderHeight()
33+
})
34+
2035
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>)
2338
}
2439

2540
export default JumpToCommentButton

0 commit comments

Comments
 (0)