彻底理解javascript 中的事件对象的pageY, clientY, screenY的区别和联系。
说到底, pageY, clientY, screenY的计算,就是要找到参考点, 它们的值就是: 鼠标点击的点----------- 和参考点指点----------的直角坐标系的距离
stackoverflow上面有个回答,讲解的非常清晰。
offsetX and offsetY are relative to the parent container, whereas pageX and pageY are relative to the document
1 2 | // rect is a DOMRect object with eight properties: left, top, right, bottom, x, y, width, height var rect = obj.getBoundingClientRect(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <! doctype html> < html > < head > < meta charset="UTF-8" /> < title >Canvas Drag and Drop Test</ title > </ head > < body > < style > .div1{ height: 3000px; background-color: yellow; } .div2{ height: 400px; background-color: lightsalmon; } .div3{ width: 200px; height: 200px; background-color: lightblue; } </ style > < div class="div1"> < div class="div2"> < div class="div3"> </ div > </ div > </ div > < script > document.getElementsByClassName('div3')[0].addEventListener('mousedown', function(e){ console.log("pageY", e.pageY); console.log("clientY", e.clientY); console.log("screenY", e.screenY); }) </ script > </ body > </ html > |
---------------------------------------------------------------------------------------------------------------------------------------
In JavaScript:
pageX, pageY, screenX, screenY, clientX and clientY returns a number which indicates the number of physical "css pixels" a point is from the reference point. The event point is where the user clicked, the reference point is a point in the upper left. These properties return the horizontal and vertical distance from that reference point.
pageX and pageY:
Relative to the top left of the fully rendered content area in the browser. This reference point is below the url bar and back button in the upper left. This point could be anywhere in the browser window and can actually change location if there are embedded scrollable pages embedded within pages and the user moves a scrollbar.
screenX and screenY:
Relative to the top left of the physical screen/monitor, this reference point only moves if you increase or decrease the number of monitors or the monitor resolution.
clientX and clientY:
Relative to the upper left edge of the content area (the viewport) of the browser window. This point does not move even if the user moves a scrollbar from within the browser.
For a visual on which browsers support which properties:
http://www.quirksmode.org/dom/w3c_cssom.html#t03
w3schools has an online Javascript interpreter and editor so you can see what each does
http://www.w3schools.com/jsref/tryit.asp?filename=try_dom_event_clientxy
<!DOCTYPE html>
<html>
<head>
<script>
function show_coords(event)
{
var x=event.clientX;
var y=event.clientY;
alert("X coords: " + x + ", Y coords: " + y);
}
</script>
</head>
<body>
<p onmousedown="show_coords(event)">Click this paragraph,
and an alert box will alert the x and y coordinates
of the mouse pointer.</p>
</body>
</html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
2017-04-12 How can we listen for errors that do not trigger window.onerror?
2017-04-12 汇编程序分析器
2017-04-12 Capture and report JavaScript errors with window.onerror
2017-04-12 js中的自定义异常处理函数
2017-04-12 javascript 自定义错误处理
2017-04-12 How do I know which version of Javascript I'm using?
2016-04-12 javascript 中获取对象的长度(map对象的长度)--js关联数组的长度