11

I'm working on a small application and I'm wondering how can I listen for the end of the scroll event.

I know that the following code allows me to listen for scroll events, but in my case I'm looking for a way to trigger an event once the user stops scrolling.

window.addEventListener('scroll', () => {
  (!this.state.showScrollWidget) ? this.setState({showScrollWidget: true}) : null;
})

If anyone has an idea about the best way to do it I will appreciate the help, otherwise if there is any third party library that may do the job I will also appreciate any suggestions.

Thanks.

3
  • maybe a interval function should help, which would check last position of scroll and current position of scroll, if they are same, then it means user is not scrolling. What library are you planning to use though? Commented May 2, 2018 at 9:27
  • yeah I know that I can do that through an interval function but I was wondering if there is any better solution, concerning the library i'm waiting for some suggestions. Commented May 2, 2018 at 9:32
  • Nope. Just use a setTimeout(). There's no point in using another library for something so trivial (if one even exists - which I doubt). Commented May 2, 2018 at 9:34

3 Answers 3

12

I don't think there's any event to notify you that the scrolling stopped. In general, you need to wait for some time to elapse until the last scroll event. Typically you would use the debounce operation for that - many different utility libs implement it, e.g. lodash (https://lodash.com/docs/4.17.10#debounce):

window.addEventListener(
  "scroll",
  _.debounce(() => {
    console.log("scrolling stopped");
  }, 1000)
);
Sign up to request clarification or add additional context in comments.

Comments

11

Solved the problem by using lodash debounce.

  1. Attach onScroll event:
 <List onScroll={e => handleScroll(e)}/>
  1. useMemo for the endScroll function:
 const handleEndScroll = useMemo(
    () =>
      _.debounce(() => console.log("END SCROLL"), 1000),
    []
  );
  1. Handle scroll and endScroll:
 const handleScroll = e => {
    console.log("scroll scroll scroll");
    handleEndScroll();
  };

Note:

  • install and import lodash
  • import useMemo

Comments

5

The scrollend event was added to FF in 109. Blink/Webkit browsers are expected to get it in mid- to late 2023.

2 Comments

Not supported in Safari or Safari for iOS as of March 2024

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.