vuejs-challenges icon indicating copy to clipboard operation
vuejs-challenges copied to clipboard

25 - 鼠标坐标

Open wangqiJava opened this issue 1 year ago • 0 comments

// your answers
<script setup lang="ts">
import { onMounted, onUnmounted, ref } from 'vue';

// Implement ...
function useEventListener(target, event, callback) {
  onMounted(()=>target.addEventListener(event,callback))
  onUnmounted(()=>target.removeEventListener(event,callback))
}

// Implement ...
function useMouse() {
  let x = ref(0)
  let y = ref(0)
  useEventListener(window, "mousemove", (e: MouseEvent) => {
    x.value = e.clientX
    y.value = e.clientY
  })
  return {x,y}
}
const { x, y } = useMouse()
</script>

<template>Mouse position is at: {{ x }}, {{ y }}</template>

wangqiJava avatar Sep 19 '24 01:09 wangqiJava