Skip to content

Commit

Permalink
feat: useWindowScroll - for cross-browser compatibility
Browse files Browse the repository at this point in the history
For cross-browser compatibility, use window.pageYOffset instead of window.scrollY.
(https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollY)
  • Loading branch information
jeemyeong committed Jul 22, 2019
1 parent 950f5ed commit 5987cc8
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/useWindowScroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ export interface State {
const useWindowScroll = (): State => {
const frame = useRef(0);
const [state, setState] = useState<State>({
x: isClient ? window.scrollX : 0,
y: isClient ? window.scrollY : 0,
x: isClient ? window.pageXOffset : 0,
y: isClient ? window.pageYOffset : 0,
});

useEffect(() => {
const handler = () => {
cancelAnimationFrame(frame.current);
frame.current = requestAnimationFrame(() => {
setState({
x: window.scrollX,
y: window.scrollY,
x: window.pageXOffset,
y: window.pageYOffset,
});
});
};
Expand Down

0 comments on commit 5987cc8

Please sign in to comment.