-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c5f405e
commit 27cdbca
Showing
4 changed files
with
36 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,4 +22,5 @@ yarn-debug.log* | |
yarn-error.log* | ||
|
||
imgs | ||
2017 | ||
2017 | ||
_export |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,32 @@ | ||
// https://stackoverflow.com/a/22599173/2255980 | ||
let scrollSpeed = '' | ||
let lastPos = 0 | ||
let newPos = 0 | ||
let delta = 0 | ||
let timer = null | ||
let timeout = null | ||
|
||
function clear() { | ||
lastPos = 0 | ||
} | ||
|
||
// TODO: need to indicate when scrolling has stopped | ||
|
||
export default function (latestYOffset, scrollThrottleMs) { | ||
export default function (latestYOffset, scrollThrottleMs, idleCallback) { | ||
newPos = latestYOffset | ||
|
||
if (lastPos !== 0) delta = newPos - lastPos | ||
if (lastPos !== 0) delta = Math.abs(newPos - lastPos) | ||
|
||
lastPos = newPos | ||
timer && clearTimeout(timer) | ||
timer = setTimeout(clear, scrollThrottleMs * 2.5) | ||
return Math.abs(delta) | ||
|
||
if (delta < 1000) { | ||
scrollSpeed = 'slow' | ||
} else if (delta < 3000) { | ||
scrollSpeed = 'medium' | ||
} else { | ||
scrollSpeed = 'fast' // only really happens when user grabs the scrollbar | ||
} | ||
|
||
// kind of like a reversed debounce, | ||
// if this function hasn't been called in a little while, fire the idleCallback function | ||
clearTimeout(timeout) | ||
timeout = setTimeout(() => { | ||
timeout = null | ||
idleCallback('slow') | ||
}, scrollThrottleMs * 2) | ||
|
||
return scrollSpeed | ||
} |