Is there a way to utilize JavaScript to change the cursor
attribute to none
after a period of mouse inactivity, like five seconds, and then revert it back to auto
once the mouse becomes active again?
EDIT: I am aware that none
is not an official value for the cursor
property. However, many web browsers seem to support it. Also, I am the primary user so confusion is unlikely to occur.
I have attempted two scripts that provide similar functionalities:
window.addEventListener("mousemove",
function(){
document.querySelector("#editor").style.background = "#000";
setTimeout("document.querySelector('#editor').style.background = '#fff'", 5000);
}
, true);
and
var timeout;
var isHidden = false;
document.addEventListener("mousemove", magicMouse);
function magicMouse() {
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(function() {
if (!isHidden) {
document.querySelector("body").style.cursor = "none";
document.querySelector("#editor").style.background = "#fff";
isHidden = true;
}
}, 5000);
if (isHidden) {
document.querySelector("body").style.cursor = "auto";
document.querySelector("#editor").style.background = "#000";
isHidden = false;
}
};
Both of these scripts change the background color to white when the mouse remains inactive for more than five seconds, and back to black when the cursor is moved. However, they do not successfully hide the cursor as intended. Interestingly, executing
document.querySelector("body").style.cursor = "none";
directly in the JavaScript console functions correctly. Unfortunately, when integrated into the scripts, it does not work.
I am sharing these scripts to demonstrate my current progress in attempting to achieve this functionality. I am not specifically requesting fixes for these scripts; rather, if you know of a more effective method to hide the cursor, please do share.