Vue function that tracks Window
scroll position.
function useWindowSize(
runOnMount?: boolean
): {
width: Ref<number>
height: Ref<number>
isTracking: Ref<boolean>
start: () => void
stop: () => void
}
runOnMount: boolean
whether to track the window resize event on mount,true
by default
width: Ref<number>
the current window'swidth
height: Ref<number>
the current window'sheight
isTracking: Ref<boolean>
whether this function observer is running or notstart: Function
the function used for start tracking window's resize eventstop: Function
the function used for stop tracking window's resize event
<template>
<div>
<div>
Window width and height: {{ width }}px {{ height }}px
</div>
<button @click="start" v-if="!isTracking">
Start tracking window resize
</button>
<button @click="stop" v-else>Stop tracking window resize</button>
</div>
</template>
<script lang="ts">
import Vue from 'vue'
import { useWindowSize } from 'vue-use-kit'
export default Vue.extend({
name: 'UseWindowSizeDemo',
setup() {
const { width, height, isTracking, start, stop } = useWindowSize()
return { width, height, isTracking, start, stop }
}
})
</script>