Skip to content

25 - 鼠标坐标 #3100

@a1185963755

Description

@a1185963755
<script setup lang="ts">
import { ref } from 'vue'

// Implement ...
function useEventListener(target: HTMLElement | Window, event: keyof HTMLElementEventMap, callback: any) {
  target.addEventListener(event, callback)
}

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

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions