Vue function that tracks the mouse x
and y
position,
and optionally tracks the mouse position relative to a given element.
function useMouseElement(elRef: Ref<null | HTMLElement>): {
docX: Ref<number>;
docY: Ref<number>;
elX: Ref<number>;
elY: Ref<number>;
elInfoX: Ref<number>;
elInfoY: Ref<number>;
elInfoW: Ref<number>;
elInfoH: Ref<number>;
}
elRef: Ref<null | HTMLElement>
used for getting thex
andy
mouse position relative to the position of the given element
docX: Ref<number>
the mousex
position relative to the documentdocY: Ref<number>
the mousey
position relative to the documentelX: Ref<number>
the mousex
position relative to the given elementelY: Ref<number>
the mousey
position relative to the given elementelInfoX: Ref<number>
the elementx
positionelInfoY: Ref<number>
the elementy
positionelInfoW: Ref<number>
the elementwidth
valueelInfoH: Ref<number>
the elementheight
value
<template>
<div>docX docY: {{ docX }}px {{ docY }}px</div>
</template>
<script lang="ts">
import Vue from 'vue'
import { useMouseElement } from 'vue-use-kit'
export default Vue.extend({
name: 'useMouseElement',
setup() {
const { docX, docY } = useMouseElement()
return { docX, docY }
}
})
</script>