I am trying to creating an arrow-up button, which will be set to display: "none"
if I am at a top of my page, and when I scroll down further, I want to set it to display: "block". How can i achieve this? I am trying to manipulate the DOM inside my handleScroll function but it doesn't work.
My TopButton.js component
import React from "react";
import "../assets/arrow-up.png";
class TopButton extends React.Component {
constructor(props) {
super(props);
this.handleScroll = this.handleScroll.bind(this);
}
componentDidMount = () => {
window.addEventListener("scroll", this.handleScroll, true);
};
handleClick = () => {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
};
handleScroll = () => {
console.log("scroll");
let x = document.getElementsByClassName("topbutton_button");
var x = document.getElementsByClassName("topbutton_button");
// x.style.display = "none";
// console.log(event.target.className);
// if (
// document.body.scrollTop > 40 ||
// document.documentElement.scrollTop > 40
// ) {
// style.display = "block";
// } else {
// display = "none";
// }
};
render() {
return (
<div className="topbutton_container">
<button
style={{ display: "block" }}
onClick={this.handleClick}
onScroll={this.handleScroll}
className="topbutton_button"
>
<img src={require("../assets/arrow-up.png")} />
</button>
</div>
);
}
}
export default TopButton;