-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scroll.jsx
35 lines (31 loc) · 971 Bytes
/
Scroll.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { FaAngleUp } from 'react-icons/fa';
import { useState, useEffect } from 'react';
import './Scroll.css';
const ScrollToTop = () => {
const [showTopBtn, setShowTopBtn] = useState(false);
useEffect(() => {
window.addEventListener('scroll', () => {
if (window.scrollY > 400) {
setShowTopBtn(true);
} else {
setShowTopBtn(false);
}
});
}, []);
const goToTop = () => {
window.scrollTo({
top: 0,
behavior: 'smooth',
});
};
return (
<div className = { `top-to-btm ${showTopBtn ? 'visible' : 'hidden'} link-primary cursor-pointer capitalize` }
onClick = { goToTop } >
{
showTopBtn && (
<FaAngleUp className="icon-position icon-style" onClick={goToTop} />
)
} </div>
);
};
export default ScrollToTop;