admin管理员组文章数量:1186428
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.
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.
Share Improve this question edited May 2, 2018 at 9:34 Rory McCrossan 337k41 gold badges319 silver badges350 bronze badges asked May 2, 2018 at 9:26 BenFarhat SouhaibBenFarhat Souhaib 3603 gold badges5 silver badges17 bronze badges 3 |3 Answers
Reset to default 12I 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)
);
Solved the problem by using lodash debounce
.
- Attach
onScroll
event:
<List onScroll={e => handleScroll(e)}/>
useMemo
for theendScroll
function:
const handleEndScroll = useMemo(
() =>
_.debounce(() => console.log("END SCROLL"), 1000),
[]
);
- Handle scroll and endScroll:
const handleScroll = e => {
console.log("scroll scroll scroll");
handleEndScroll();
};
Note:
- install and import
lodash
- import
useMemo
The scrollend
event was added to FF in 109. Blink/Webkit browsers are expected to get it in mid- to late 2023.
本文标签: javascriptReactjs Listen for scroll end eventsStack Overflow
版权声明:本文标题:javascript - Reactjs Listen for scroll end events - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738356579a2079944.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
setTimeout()
. There's no point in using another library for something so trivial (if one even exists - which I doubt). – Rory McCrossan Commented May 2, 2018 at 9:34