侧边栏壁纸
博主头像
前端学习

行动起来,活在当下

  • 累计撰写 307 篇文章
  • 累计创建 18 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

关于鼠标事件相对于浏览器位置

Administrator
2022-05-11 / 0 评论 / 0 点赞 / 122 阅读 / 0 字

clientX当鼠标事件发生的时候,鼠标相对于浏览器左侧的距离
clientY当鼠标事件发生的时候,鼠标相对于浏览器Y轴的位置

<div id="box"></div> 
<div id="wrap"></div>
var box = document.querySelector('#box');
document.onclick = function (e) {
  console.log(e);
  // 1.关于鼠标事件相对于浏览器位置的
  // clientX当鼠标事件发生的时候,鼠标相对于浏览器左侧的距离
  // clientY当鼠标事件发生的时候,鼠标相对于浏览器Y轴的位置
  box.innerHTML = e.clientX + '|' + e.clientY;
}
var wrap = document.querySelector('#wrap');
wrap.onclick = function (e) {
  console.log(e);
  this.innerHTML = e.offsetX + '|' + e.offsetY;
}
#box {
  width: 200px;
  border: 1px solid red;
  font-weight: bolder;
}

#wrap {
  width: 360px;
  height: 360px;
  background-color: skyblue;
  margin: 100px 0;
  font-size: 18px;
}

代码演示:

202104071617778788148753

0

评论区