You need to enable JavaScript to run this app. JavaScript实现跟随鼠标的圆形在不同div内的位置检测与颜色切换
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript实现跟随鼠标的圆形在不同div内的位置检测与颜色切换

跟随鼠标的交互圆形元素实现指南

核心逻辑拆解

要实现需求只需要把流程拆成四个可独立测试的模块,不需要引入额外依赖:

  • 圆形跟随基础:通过全局mousemove事件监听鼠标位置,把圆形的定位锚点和鼠标指针重合,实现无延迟跟随
  • 位置边界检测:通过元素原生的getBoundingClientRect()方法拿到两个背景容器的实时可视边界,通过坐标比对判断圆形当前的所属区域
  • 动态颜色切换:根据区域检测结果,直接修改圆形元素的背景色,严格匹配需求的触发规则
    • 圆形在黑色背景容器内 → 填充色为红色
    • 圆形在红色背景容器内 → 填充色为黑色
    • 圆形在两个容器外 → 保持默认蓝色
  • 实时信息展示:每次鼠标位置更新时,同步把当前所属区域、圆形坐标渲染到页面展示区,做到状态和画面1:1同步

可直接运行的代码参考

HTML 结构

<!-- 两个指定背景容器 -->
<div class="bg-box black"></div>
<div class="bg-box red"></div>

<!-- 跟随鼠标的圆形元素 -->
<div id="follow-circle"></div>

<!-- 位置信息实时展示区 -->
<div id="pos-tip"></div>

CSS 样式

/* 两个背景容器的基础样式 */
.bg-box {
  width: 300px;
  height: 200px;
  margin: 30px;
}
.bg-box.black {
  background: #000;
}
.bg-box.red {
  background: #e63946;
}

/* 跟随圆形的基础样式 */
#follow-circle {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: #1d4ed8; /* 默认蓝色 */
  position: fixed;
  left: 0;
  top: 0;
  z-index: 9999;
  pointer-events: none; /* 关键:避免圆形拦截鼠标事件 */
  transform: translate(-50%, -50%); /* 让圆心精准对齐鼠标指针 */
}

#pos-tip {
  position: fixed;
  bottom: 20px;
  left: 20px;
  padding: 8px 12px;
  background: #f1f5f9;
  border-radius: 4px;
}

JavaScript 核心交互逻辑

const circleDom = document.getElementById('follow-circle');
const tipDom = document.getElementById('pos-tip');
const blackBoxDom = document.querySelector('.bg-box.black');
const redBoxDom = document.querySelector('.bg-box.red');

// 全局监听鼠标移动
window.addEventListener('mousemove', (event) => {
  const currentX = event.clientX;
  const currentY = event.clientY;

  // 更新圆形位置
  circleDom.style.transform = `translate(${currentX}px, ${currentY}px) translate(-50%, -50%)`;

  // 获取两个容器的实时可视边界
  const blackRect = blackBoxDom.getBoundingClientRect();
  const redRect = redBoxDom.getBoundingClientRect();

  // 坐标比对判断所属区域
  let areaDesc = '处于两个容器外部';
  let circleBg = '#1d4ed8'; // 默认蓝色

  // 判断是否在黑容器内
  const isInBlack = currentX >= blackRect.left 
    && currentX <= blackRect.right 
    && currentY >= blackRect.top 
    && currentY <= blackRect.bottom;
  
  // 判断是否在红容器内
  const isInRed = currentX >= redRect.left 
    && currentX <= redRect.right 
    && currentY >= redRect.top 
    && currentY <= redRect.bottom;

  if (isInBlack) {
    areaDesc = '处于黑色背景容器内部';
    circleBg = '#e63946'; // 红色
  } else if (isInRed) {
    areaDesc = '处于红色背景容器内部';
    circleBg = '#000'; // 黑色
  }

  // 更新圆形颜色
  circleDom.style.background = circleBg;

  // 更新位置提示文本
  tipDom.textContent = `${areaDesc} | 圆心坐标:(${currentX}, ${currentY})`;
})

实现时需要注意的细节

  • 必须给跟随圆形加上pointer-events: none,不然圆形会成为鼠标事件的拦截层,导致容器的hover事件、点击事件失效,出现交互卡顿
  • 不要提前缓存容器的边界值,每次鼠标移动时实时调用getBoundingClientRect()获取最新边界,避免页面滚动、窗口缩放后位置检测出现偏移
  • 用transform做位置偏移比直接修改left/top性能更好,不会触发浏览器重排,跟随鼠标的流畅度会更高
  • 如果两个容器存在重叠区域,可根据实际需求调整判断优先级,避免状态冲突

内容的提问来源于stack exchange,提问作者nima hashemi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.11 02:15:05