Skip to content

Instantly share code, notes, and snippets.

@scmx
Forked from josephwegner/jquery-hideIdleCursor.js
Last active December 17, 2022 05:27
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scmx/1f79adde2e9c69912fee520a246ec9e5 to your computer and use it in GitHub Desktop.
Save scmx/1f79adde2e9c69912fee520a246ec9e5 to your computer and use it in GitHub Desktop.
Hide your cursor on a web page when it is idle. Great if you're using a web page for some sort of TV Display and don't want the cursor messing things up. #javascript #cursor #css #idle #hide

Hide your cursor on a web page when it is idle

Great if you're using a web page for some sort of TV Display and don't want the cursor messing things up

document.addEventListener("DOMContentLoaded", () => {
  let idleMouseTimer;
  let forceMouseHide = false;

  document.body.style.cursor = "none";

  // Your wrapper here
  document.body.addEventListener("mousemove", () => {
    if (forceMouseHide) {
      return;
    }

    document.body.style.cursor = "";

    clearTimeout(idleMouseTimer);

    idleMouseTimer = setTimeout(() => {
      document.body.style.cursor = "none";

      forceMouseHide = true;

      setTimeout(() => {
        forceMouseHide = false;
      }, 200);
    }, 1000);
  });
});

This is same as josephwegner/jquery-hideIdleCursor.js, but removed need for jQuery and ported to ES6 syntax.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment