赞
踩
import { ref, onMounted, onUnmounted } from "vue";
export function useMousePosition() {
const x = ref(0);
const y = ref(0);
function handleMouseMove(event: MouseEvent) {
x.value = event.clientX;
y.value = event.clientY;
}
onMounted(() => {
document.addEventListener("mousemove", handleMouseMove);
});
onUnmounted(() => {
document.removeEventListener("mousemove", handleMouseMove);
});
return { x, y };
}
<template>
<div>
<p>鼠标当前位置:({{ position.x }}, {{ position.y }})</p>
</div>
</template>
<script>
import { useMousePosition } from "@/hooks/useMousePosition";
export default {
setup() {
const position = useMousePosition();
return { position };
},
};
</script>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。