Vue function that tracks an HTML element's scroll position.
function useScroll(
elRef: Ref<null | HTMLElement | Window>,
ms?: number,
runOnMount?: boolean
): {
x: Ref<number>
y: Ref<number>
isTracking: Ref<boolean>
isScrolling: Ref<boolean>
start: () => void
stop: () => void
}
elRef: Ref<null | HTMLElement | Window>
target element used for tracking thex
andy
scroll positionms: number
how many milliseconds of delay beforeisScrolling
goes back to false (basically when user is idle),150
by defaultrunOnMount: boolean
whether to run the scroll tracking on mount,true
by default
x: Ref<number>
thex
scroll position relative to the elRefy: Ref<number>
they
scroll position relative to the elRefisScrolling: Ref<boolean>
whether the element is currently being scrolled or notisTracking: Ref<boolean>
whether this function events are running or notstart: Function
the function used for starting the scroll trackingstop: Function
the function used for stopping the scroll tracking
<template>
<div>
<div>x, y: {{ x }}px - {{ y }}px</div>
<div>isScrolling: {{ isScrolling }}</div>
<button @click="start" v-if="!isTracking">Start tracking</button>
<button @click="stop" v-else>Stop tracking</button>
<div ref="scrollRef" class="scrollme">
<div class="scrollme__height"></div>
</div>
</div>
</template>
<script lang="ts">
import Vue from 'vue'
import { useScroll } from 'vue-use-kit'
import { ref } from '@src/api'
export default Vue.extend({
name: 'useScrollDemo',
setup() {
const scrollRef = ref(null)
const { x, y, isScrolling, isTracking, start, stop } = useScroll(scrollRef)
return { scrollRef, x, y, isScrolling, isTracking, start, stop }
}
})
</script>
<style scoped>
.scrollme {
position: relative;
overflow-y: scroll;
height: 200px;
background: #f1f1f1;
}
.scrollme__height {
width: 2000px;
height: 40000px;
}
</style>