I am building a landing page with a lot of animations. When I need to animate something I create an event listener to the scroll and attach onScroll function, which triggers my animations by setting state.
I currently create less than 10 listeners with 10 functions to check the state.
I would like to create an entity that uses 1 event listener and then all my landing page components should subscribe and get position.
How can I do this? I am using ES6 and React.
A bit of my code:
componentDidMount() {
this.toggleAppBar();
addScrollEventListener(this.handleOnTopScroll);
addScrollEventListener(this.handleFirstScroll);
}
handleOnTopScroll = () => {
let scrollTop = document.body.scrollTop
if (scrollTop === 0) {
this.toggleAppBar();
}
}
handleFirstScroll = () => {
let scrollTop = document.body.scrollTop
//console.log(scroll);
if (!scroll) {
this.toggleAppBar();
scroll = true
return
}
if (scrollTop === 0) {
scroll = false
}
}
Solution works as it is required but I would need to add a bunch more listeners for other components
scroll()events. Ref: api.jquery.com/scrollwindow.onscrollevent. developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/…