Skip to content

Instantly share code, notes, and snippets.

@weianofsteel
Created December 4, 2020 10:03
Show Gist options
  • Save weianofsteel/1c3445ab0f215b921d154287722d8647 to your computer and use it in GitHub Desktop.
Save weianofsteel/1c3445ab0f215b921d154287722d8647 to your computer and use it in GitHub Desktop.
Scroll up button
import React from 'react';
import Button from '@material-ui/core/Button';
import ExpandLessIcon from '@material-ui/icons/ExpandLess';
class ScrollUpButton extends React.Component {
constructor(props) {
super(props);
this.state = {
scrollC: 0
};
this.handleScrolltoTop = this.handleScrolltoTop.bind(this);
this.onScroll = this.onScroll.bind(this);
}
componentDidMount() {
window.addEventListener("scroll", this.onScroll);
}
onScroll() {
this.setState({
scrollC: window.scrollY
});
}
handleScrolltoTop() {
if(window !== undefined) {
window.scrollTo({top: 0, behavior: (this.props.behavior?this.props.behavior:'auto')})
}
}
render() {
const {
IconSize,
appearCoordinate,
} = this.props;
return(
<React.Fragment>
<div>
{this.state.scrollC > (appearCoordinate?appearCoordinate:1200) ?
<Button
onClick={this.handleScrolltoTop}
>
<ExpandLessIcon style={{fontSize:IconSize?IconSize:'60px'}}/>
</Button>:''
}
</div>
</React.Fragment>
)
}
}
export default ScrollUpButton;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment