如果页面在反应中滚动,我需要在元素上添加或删除类我写了这样一个代码来跟踪页面滚动:
export default class TestComponenet extends React.Component {
constructor(props) {
super(props);
autoBind(this);
this.state = {
scrolled: false
}
}
componentDidMount() {
window.addEventListener('scroll', this.handleScroll);
};
componentWillUnmount() {
window.removeEventListener('scroll', this.handleScroll);
};
handleScroll(event) {
this.setState({srolled: true});
};
render() {
return (
<div className ={scrolled ? 'scrolling' : ''}></div>
);
}
}
但我只能跟踪滚动,但不能动态切换类。