MouseEvent.screenX

 screenX 是 MouseEvent 的只读属性,提供鼠标在全局(屏幕)中的水平坐标(偏移量)。

语法

var pixelNumber = instanceOfMouseEvent.screenX

返回值

一个双精度浮点值。早期版本的规范将该值定义为整数值的像素数。有关详细信息,请参见浏览器兼容性部分。

示例

这个例子展示了当触发 mousemove 事件时鼠标的坐标。

HTML

<p>Move your mouse to see its position.</p>
<p id="screen-log"></p>

JavaScript

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}`;
}

结果

规范

规范 状态 注释
CSS Object Model (CSSOM) View Module
screenX
Working Draft Redefines MouseEvent from long to double. 
Document Object Model (DOM) Level 3 Events Specification
MouseEvent.screenX
Obsolete No change from Document Object Model (DOM) Level 2 Events Specification.
Document Object Model (DOM) Level 2 Events Specification
MouseEvent.sceenX
Obsolete Initial definition

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
screenXChrome Full support YesEdge Full support 12Firefox Full support YesIE Full support 9Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
Value type changed from long to doubleChrome Full support 56Edge Full support ≤79Firefox ? IE ? Opera ? Safari ? WebView Android Full support 56Chrome Android Full support 56Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android Full support 6.0

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown

参见