【VUE】封装一个追随鼠标的漂浮组件框架

红色箭头代表鼠标位置,蓝色区域跟随鼠标出现,鼠标进行其他操作的时候,蓝色区域隐藏。

vue全码

复制代码
<template>
  <div
    @mousemove="updatePosition"
    @mouseleave="hideDiv"
    class="container"
    :style="{ position: 'relative' }"
  >
    <div
      v-if="showDiv"
      :style="{
        position: 'absolute',
        top: `${yPosition}px`,
        left: `${xPosition}px`,
        width: '100px',
        height: '100px',
        backgroundColor: 'lightblue'
      }"
    >1111111111
      <!-- 这里是你想要显示的内容 -->
    </div>
    <!-- 这里是页面的其他内容 -->
  </div>
</template>

<script>
export default {
  name: "SelectMenu",
  data() {
    return {
      xPosition: 0,
      yPosition: 0,
      showDiv: false
    };
  },
  methods: {
    updatePosition(event) {
      this.xPosition = event.clientX;
      this.yPosition = event.clientY;
      this.showDiv = true;
    },
    hideDiv() {
      this.showDiv = false;
    }
  }
}
</script>

<style scoped>
.container {
  height: 500px; /* 可以根据需要调整高度 */
}
</style>
相关推荐
春日见3 小时前
win11 分屏设置
java·开发语言·驱动开发·docker·单例模式·计算机外设
TESmart碲视8 小时前
如何用一套键盘鼠标切换多个输入设备?TESmart KVM热键工作原理深度解析.一份涵盖设计原理、使用方法与兼容性的完整技术指南
游戏·macos·计算机外设·kvm切换器·双屏kvm切换器
JosieBook8 小时前
【Vue】13 Vue技术—— Vue 中的键盘事件处理详解
javascript·vue.js·计算机外设
Hody911 天前
【XR开发系列】与玩家交互 - 用键盘控制小球移动
计算机外设·交互
一起养小猫3 天前
Flutter for OpenHarmony 实战:双控制系统实现(按钮+键盘)
android·flutter·计算机外设·harmonyos
cong*3 天前
搜狗输入法云计算代理导致Windows 10鼠标指针不停转圈的解决方案
windows·计算机外设
永霖光电_UVLED4 天前
光学计量工具瞄准 AR/VR、显示器和光源
计算机外设·ar·vr
SmartRadio4 天前
基于泰山派PiKVM的多电脑KVM共享方案(HDMI采集+虚拟USB键鼠+无缝切换+剪贴板/文件共享)
网络·lora·计算机外设·电脑
_codemonster4 天前
两台以上的电脑共用一套键盘鼠标(windows+Ubuntu)
windows·计算机外设·电脑
SmartRadio5 天前
ESP32-S3实现KVM远控+云玩功能 完整方案
运维·python·计算机外设·esp32·kvm·云玩