MDN self-hosted library · e01.org

Document: pointerLockElement property

Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.

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