12345678910111213141516171819202122232425262728 |
- import { ref, onMounted, onUnmounted } from 'vue';
- const getScrollY = () => document?.documentElement?.scrollTop || document?.body?.scrollTop;
- export const useScroll = () => {
- const scrollY = ref(0);
- const handleScroll = () => {
- scrollY.value = getScrollY();
- };
- onMounted(() => {
- if (process.client) {
- scrollY.value = getScrollY();
- window.addEventListener('scroll', handleScroll);
- }
- });
- onUnmounted(() => {
- if (process.client) {
- window.removeEventListener('scroll', handleScroll);
- }
- });
- return {
- scrollY
- };
- }
|