2019-08-09 10:26:22 +00:00
|
|
|
import throttle from "lodash-es/throttle";
|
2019-06-19 14:40:52 +00:00
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
|
|
|
|
function getPosition() {
|
|
|
|
return {
|
|
|
|
x: window.pageXOffset,
|
|
|
|
y: window.pageYOffset
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function useScroll() {
|
|
|
|
const [scroll, setScroll] = useState(getPosition);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const handleScroll = throttle(() => setScroll(getPosition()), 250);
|
|
|
|
|
|
|
|
window.addEventListener("scroll", handleScroll);
|
|
|
|
|
|
|
|
return () => window.removeEventListener("scroll", handleScroll);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return scroll;
|
|
|
|
}
|
|
|
|
export default useScroll;
|