forked from X-lab2017/open-leaderboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: set the display time of the scroll top button
* Fixed scroll to top Signed-off-by: Apurv Sonawane <[email protected]> * Revert "Fixed scroll to top" This reverts commit 5b3dca5. * Fixed scroll to top Signed-off-by: Apurv Sonawane <[email protected]> * Formatted with prettier Signed-off-by: Apurv Sonawane <[email protected]> * added prop Signed-off-by: Apurv Sonawane <[email protected]> * chore: run yarn prettier --------- Signed-off-by: Apurv Sonawane <[email protected]> Co-authored-by: AndyHuang <[email protected]>
- Loading branch information
1 parent
109e9be
commit 5817d8b
Showing
2 changed files
with
33 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,46 @@ | ||
import { useState, useEffect } from 'react'; | ||
import { Button } from 'antd'; | ||
import { VerticalAlignTopOutlined } from '@ant-design/icons'; | ||
function scrollToTop(timestamp) { | ||
let scrollTop = document.documentElement.scrollTop || document.body.scrollTop; | ||
window.scrollTo({ | ||
top: 0, | ||
left: 0, | ||
behavior: 'smooth', | ||
}); | ||
} | ||
const ScrollTopButton = (props) => { | ||
const [isVisible, setIsVisible] = useState(false); | ||
|
||
useEffect(() => { | ||
const handleScroll = () => { | ||
const scrollY = window.scrollY; | ||
setIsVisible(scrollY > 200); | ||
}; | ||
|
||
window.addEventListener('scroll', handleScroll); | ||
|
||
return () => { | ||
window.removeEventListener('scroll', handleScroll); | ||
}; | ||
}, []); | ||
return ( | ||
<Button | ||
style={{ | ||
position: 'fixed', | ||
zIndex: 2147483640, | ||
right: '22px', | ||
bottom: '60px', | ||
backgroundColor: '#FFCC19', | ||
borderColor: '#FFCC19', | ||
}} | ||
type="primary" | ||
icon={<VerticalAlignTopOutlined />} | ||
onClick={() => { | ||
console.log('hi'); | ||
window.requestAnimationFrame(scrollToTop); | ||
}} | ||
/> | ||
<> | ||
{isVisible && ( | ||
<Button | ||
style={{ | ||
position: 'fixed', | ||
zIndex: 2147483640, | ||
right: '22px', | ||
bottom: '60px', | ||
backgroundColor: '#FFCC19', | ||
borderColor: '#FFCC19', | ||
}} | ||
type="primary" | ||
icon={<VerticalAlignTopOutlined />} | ||
onClick={scrollToTop} | ||
/> | ||
)} | ||
</> | ||
); | ||
}; | ||
export default ScrollTopButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters