--- title: MouseEvent.screenX slug: Web/API/MouseEvent/screenX tags: - API - MouseEvent - Property - events - 事件 - 参考 - 属性 - 鼠标 - 鼠标事件 translation_of: Web/API/MouseEvent/screenX ---
{{APIRef("DOM Events")}}
screenX
是 {{domxref("MouseEvent")}} 的只读属性,提供鼠标在全局(屏幕)中的水平坐标(偏移量)。
var pixelNumber = instanceOfMouseEvent.screenX
一个双精度浮点值。早期版本的规范将该值定义为整数值的像素数。有关详细信息,请参见浏览器兼容性部分。
这个例子展示了当触发 {{Event("mousemove")}} 事件时鼠标的坐标。
<p>Move your mouse to see its position.</p> <p id="screen-log"></p>
let screenLog = document.querySelector('#screen-log'); document.addEventListener('mousemove', logKey); function logKey(e) { screenLog.innerText = ` Screen X/Y: ${e.screenX}, ${e.screenY} Client X/Y: ${e.clientX}, ${e.clientY}`; }
{{EmbedLiveSample("Example")}}
规范 | 状态 | 注释 |
---|---|---|
{{SpecName('CSSOM View','#dom-mouseevent-screenx', 'screenX')}} | {{Spec2('CSSOM View')}} | Redefines {{domxref("MouseEvent")}} from long to double. |
{{SpecName('DOM3 Events','#widl-MouseEvent-screenX','MouseEvent.screenX')}} | {{Spec2('DOM3 Events')}} | No change from {{SpecName('DOM2 Events')}}. |
{{SpecName('DOM2 Events','#Events-MouseEvent','MouseEvent.sceenX')}} | {{Spec2('DOM2 Events')}} | Initial definition |
{{Compat("api.MouseEvent.screenX")}}