-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScrollTop.js
59 lines (49 loc) · 1.48 KB
/
ScrollTop.js
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import {h, Component} from "preact";
import { SVG } from "./Icon";
import "./scrollTop.scss";
const chevronUp = <polyline points="18 15 12 9 6 15" />;
export default class ScrollTop extends Component {
state = { intervalId: 0, hide: true };
componentDidMount()
{
window.addEventListener('scroll', this.handleScroll);
}
componentWillUnmount()
{
window.removeEventListener('scroll', this.handleScroll);
}
handleScroll = () => {
if (window.scrollY === 0 || window.scrollY <= 400 ) {
this.setState({hide: true});
} else {
this.setState({hide: false});
}
}
scrollStep = () => {
if (window.pageYOffset === 0) {
clearInterval(this.state.intervalId);
}
window.scroll(0, window.pageYOffset - this.props.scrollStepInPx);
}
scrollTop = () =>
{
let intervalId = setInterval(this.scrollStep, this.props.delayInMs);
this.setState({ intervalId: intervalId });
}
render()
{
return (
<div className={`go-to${this.state.hide ? '': ' show'}`}>
<button className="btn btn-primary rounded-circle border-0 p-3" onClick={this.scrollTop}>
{/*<Icon name="chevron-up" type="" />*/}
<SVG icon={this.props.icon} />
</button>
</div>
);
}
}
ScrollTop.defaultProps = {
scrollStepInPx: "100",
delayInMs: "8",
icon: chevronUp
};