-
Notifications
You must be signed in to change notification settings - Fork 15
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
lmhcoding
committed
Sep 19, 2020
1 parent
9bc82da
commit 70b2a3a
Showing
3 changed files
with
43 additions
and
12 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './useTitle' | ||
export * from './useEvent' | ||
export * from './useScroll' |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { reactive, toRefs, ToRefs } from 'vue' | ||
import { useEvent, Target } from './useEvent' | ||
|
||
export type ScrollState = ToRefs<{ | ||
x: number | ||
y: number | ||
}> | ||
|
||
export function useScroll(target: Target): ScrollState { | ||
if (target === window && process.env.NODE_ENV !== 'production') { | ||
throw Error('target of useScroll should not be window') | ||
} | ||
const state = reactive({ | ||
x: 0, | ||
y: 0 | ||
}) | ||
const eventTarget = useEvent( | ||
'scroll', | ||
() => { | ||
if (eventTarget.value) { | ||
state.x = ((eventTarget.value as unknown) as Element).scrollLeft | ||
state.y = ((eventTarget.value as unknown) as Element).scrollTop | ||
} | ||
}, | ||
{ capture: false, passive: true }, | ||
target | ||
) | ||
return toRefs(state) | ||
} |