Answer the question
In order to leave comments, you need to log in
How to catch the moment when the scroll direction changes?
componentDidMount() {
this.lastScrollPos = getScrollTop();
window.addEventListener('scroll', this.scrollUpDown);
}
componentWillUnmount() {
window.removeEventListener('scroll', this.scrollUpDown);
}
scrollUpDown() {
const scroll = this.lastScrollPos;
this.lastScrollPos = getScrollTop();
if (scroll < this.lastScrollPos) {
this.setState({shownMenu: false});
console.log('скролим вниз');
return;
}
if (scroll > this.lastScrollPos) {
this.setState({ shownMenu: true});
console.log('скролим вниз');
}
}
Answer the question
In order to leave comments, you need to log in
I did it like this, depending on the direction of the scroll, the menu is shown or disappears, it also uses a delay of 150px for mobile and 300 px for the rest of the responders
scrollUpDown() {
const isMobile = this.getLayout();
const delay = isMobile ? 150 : 300;
const scroll = this.lastScrollPos;
this.lastScrollPos = getScrollTop();
const direction = this direction; // old direction value
this.direction = (scroll < this.lastScrollPos);
if (direction !== this.direction) {
this.scrollUp = this.lastScrollPos;// scroll value when changing direction
}
if (Math.abs(this.scrollUp - this.lastScrollPos) > delay) {
this.setState({shownMenu: !this.direction});
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question