MouseEvent: clientX-Eigenschaft

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Die clientX-Eigenschaft des MouseEvent-Interfaces (nur lesbar) liefert die horizontale Koordinate innerhalb des Viewports einer Anwendung, an der das Ereignis aufgetreten ist (im Gegensatz zur Koordinate innerhalb der Seite).

Zum Beispiel, das Klicken auf den linken Rand des Viewports führt immer zu einem Mausereignis mit einem clientX-Wert von 0, unabhängig davon, ob die Seite horizontal gescrollt wurde.

Wert

Ein double-Fließkommawert in Pixeln.

Beispiele

Dieses Beispiel zeigt die Koordinaten Ihrer Maus, wenn Sie das mousemove-Ereignis auslösen.

HTML

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

JavaScript

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

Ergebnis

Spezifikationen

Specification
UI Events
# dom-mouseevent-clientx

Browser-Kompatibilität

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
clientX

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

Siehe auch