1.自定义方法
import { reactive, onMounted, onUnmounted } from 'vue'
export function MouseMovement() {
let mouse = reactive({ x: 0, y: 0 })
function update(e) {
mouse.x = e.pageX;
mouse.y = e.pageY;
}
onMounted(() => {
window.addEventListener("mousemove", update);
})
onUnmounted(() => {
window.removeEventListener("mousemove", update);
})
return mouse
}
2.调用方法
<template>
<img :src="'xxx.png'" alt=""
:style="'left:' + (mousePosition.x + 10) + 'px;top:' + (mousePosition.y + 10) + 'px;position: fixed;'">
</template>
<script setup>
import { MouseMovement } from './json'
const mousePosition = MouseMovement(); // 获取鼠标位置
</script>
Comments | NOTHING