-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
33 lines (25 loc) · 980 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*!
* Run a callback function after scrolling has stopped
* (c) 2017 Chris Ferdinandi, MIT License, https://gomakethings.com
* @param {Function} callback The callback function to run after scrolling
* @param {Integer} refresh How long to wait between scroll events [optional]
*/
function scrollStop(node = window, callback, refresh = 66) {
// Make sure a valid callback was provided
if (!callback || typeof callback !== 'function') return
// Setup scrolling variable
let isScrolling
const target = 'current' in node ? node.current : node
const listener = function (event) {
// Clear our timeout throughout the scroll
window.clearTimeout(isScrolling)
// Set a timeout to run after scrolling ends
isScrolling = setTimeout(callback, refresh)
}
// Listen for scroll events
target.addEventListener('scroll', listener, false)
return () => {
target.removeEventListener('scroll', listener, false)
}
}
module.exports = scrollStop