相关疑难解决方法(0)

使用JavaScript在空闲时隐藏鼠标光标

如果鼠标在一段时间内(例如,五秒钟)处于非活动状态并且将其重新设置为再次激活时,是否可以使用JavaScript将cursor属性设置为属性?noneauto

编辑:我意识到这none不是该cursor属性的有效值.尽管如此,许多网络浏览器似乎都支持它.此外,这方面的主要用户是我自己,因此几乎不会出现混淆的可能性.

我有两个可以做类似的脚本:

window.addEventListener("mousemove",
    function(){
        document.querySelector("#editor").style.background = "#000";
        setTimeout("document.querySelector('#editor').style.background = '#fff'", 5000);
    }
, true);
Run Code Online (Sandbox Code Playgroud)

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;
    }
};
Run Code Online (Sandbox Code Playgroud)

对于其中的每一个,当鼠标处于非活动状态超过五秒时,背景颜色变为白色,而当光标移动时,背景变为黑色.但是,它们不能使光标消失.让我感到惊讶的是,如果我将命令document.querySelector("body").style.cursor = "none";放入JavaScript控制台,它就能完美运行.在脚本中,它似乎不起作用.

我已经发布了上面的脚本,因为这是我开始使用它.我不一定要求修复任何一个脚本; …

javascript css cursor mousemove

9
推荐指数
2
解决办法
2万
查看次数

标签 统计

css ×1

cursor ×1

javascript ×1

mousemove ×1