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

25 - 鼠标坐标

Open kalu5 opened this issue 2 years ago • 1 comments

// 你的答案
<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
// Implement ...
function useEventListener(target, event, callback) {
  onMounted(() => {
    target.addEventListener(event, callback, false)
  })
  
  onUnmounted(() => {
    target.removeEventListener(event, callback, false)
  })
}

// Implement ...
function useMouse() {
  let x = ref<number>(0)
  let y = ref<number>(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>

kalu5 avatar Sep 19 '22 01:09 kalu5

there is no need to annotate the type

undefined-zzk avatar Jan 30 '24 08:01 undefined-zzk