The read-only pointerLockElement property of the Document interface provides the element set as the target for mouse events while the pointer is locked. It is null if lock is pending, pointer is unlocked, or the target is in another document.
The read-only pointerLockElement property of the Document interface provides the element set as the target for mouse events while the pointer is locked. It is null if lock is pending, pointer is unlocked, or the target is in another document.
An Element or null.
This example contains a <div> element that in turn contains a <button>. Clicking the button requests pointer lock for the <div>.
The example also listens for the pointerlockchange event: when this event is fired, the event handler disables the "Lock" button if an element in the document has the pointer lock, and enables the button otherwise.
The effect of this is that if you click the "Lock" button, the pointer is locked and the button is disabled: if you then exit pointer lock (for example, by pressing the Escape key), the button is enabled again.
html
<div id="container"> <button id="lock">Lock</button> </div>
css
div { height: 100px; width: 200px; border: 2px solid blue; }
js
const lock = document.querySelector("#lock"); const container = document.querySelector("#container"); lock.addEventListener("click", () => { container.requestPointerLock(); }); document.addEventListener("pointerlockchange", () => { const locked = document.pointerLockElement; lock.disabled = Boolean(locked); });
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
pointerLockElement |
37 | 13 | 5014–50 | No | 24 | 10.1 | 37 | 37 | 5014–50 | 24 | No | 3.0 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Document/pointerLockElement