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;
}
JavaScript
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);
});
Result
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4