react滚动到一定位置显示内容

const initState = {
	openShow:false,
}
class Header extends Component {
	constructor(props) {
	   super(props);
	   this.state = initState;
	   this.activeNavTab=this.activeNavTab.bind(this)
	}
	componentDidMount= async () =>{
	     window.addEventListener("scroll", this.activeNavTab, {
	         passive: true,
	     });
	 }
	activeNavTab() {
	   // console.log(window.scrollY);
	   if (window.scrollY < 245) {
	       this.setState(() =>({
	           openShow:false
	       }))
	   } else {
	       this.setState(() =>({
	           openShow:true
	       }))
	   }
	}
	 render() {
        // console.log(this.state.nftContract)
        return (
            <div>
                <button onClick={this.backTop} className={ `scroll-top scroll-to-target ${this.state.openShow ? "open" : ""}` }>
                    <FiArrowUp/>
                </button>
            </div>
        );
     }
}

你可能感兴趣的:(React,react.js,javascript,前端)