forked from pu428f3pz/InternshipProject
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.3 KiB
38 lines
1.3 KiB
import { useRef } from 'react';
|
|
export default (function (isScrollAtTop, isScrollAtBottom) {
|
|
// Do lock for a wheel when scrolling
|
|
var lockRef = useRef(false);
|
|
var lockTimeoutRef = useRef(null);
|
|
|
|
function lockScroll() {
|
|
clearTimeout(lockTimeoutRef.current);
|
|
lockRef.current = true;
|
|
lockTimeoutRef.current = setTimeout(function () {
|
|
lockRef.current = false;
|
|
}, 50);
|
|
} // Pass to ref since global add is in closure
|
|
|
|
|
|
var scrollPingRef = useRef({
|
|
top: isScrollAtTop,
|
|
bottom: isScrollAtBottom
|
|
});
|
|
scrollPingRef.current.top = isScrollAtTop;
|
|
scrollPingRef.current.bottom = isScrollAtBottom;
|
|
return function (deltaY) {
|
|
var smoothOffset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
var originScroll = // Pass origin wheel when on the top
|
|
deltaY < 0 && scrollPingRef.current.top || // Pass origin wheel when on the bottom
|
|
deltaY > 0 && scrollPingRef.current.bottom;
|
|
|
|
if (smoothOffset && originScroll) {
|
|
// No need lock anymore when it's smooth offset from touchMove interval
|
|
clearTimeout(lockTimeoutRef.current);
|
|
lockRef.current = false;
|
|
} else if (!originScroll || lockRef.current) {
|
|
lockScroll();
|
|
}
|
|
|
|
return !lockRef.current && originScroll;
|
|
};
|
|
}); |