【问题标题】:Hide the cursor and make it stop moving using Javascript使用 Javascript 隐藏光标并使其停止移动
【发布时间】:2026-02-15 16:15:01
【问题描述】:

我正在制作一款 3D 游戏,其中相机需要与魔兽世界中的相机几乎相同。这意味着,一旦点击屏幕,光标就会消失,而当鼠标移动时,相机会围绕焦点(播放器)旋转。

我编写了大部分代码,单击屏幕时光标确实消失了,但问题是它仍在移动,即使它没有显示出来。这感觉不自然,我想让光标一直停留在同一个位置。

那么,我该如何使用 Javascript 来实现呢?

唯一的支持要求是最新版本的 Chrome 和 Firefox。

【问题讨论】:

  • 类似问题在这里:link

标签: javascript html browser cursor


【解决方案1】:

您不能像在 JavaScript 中那样操纵光标的位置,因为它会带来安全隐患。

【讨论】:

  • 正如我所说,我知道如何隐藏光标。 document.body.style.cursor = "none" 成功了。
  • @Bane 啊,那么你的位置问题的答案是,由于浏览器安全限制,你不能操纵光标位置:)
  • @Bane 是的,我同意,但想象一下那些垃圾邮件网站会如何控制您的光标?不好:)
  • @TrevorSenior 好问题!我只是做了一些谷歌搜索,但只找到了如何更改光标图像,not 位置。可能是另一个安全限制?
【解决方案2】:

如果我的问题没猜错

无法通过 javascript,您需要使用 flash。

但是是的,一些进展确实在进行中,

Pointer lock api

更新:(我有空闲时间,所以我玩了它)

您可以尝试这样的方法,它并不完美,不推荐,并且在原始鼠标到达屏幕边框时失败(但是您可以在包装器中限制鼠标移动,这将解决问题)。

HTML:

<body style='width:100%;min-height:800px;overflow:hidden'>  
<img id='fakeCursor' src='http://i50.tinypic.com/359d3km.jpg'  style='z-index:1000;position:absolute;display:none' />  
<div href='javascript:void(0);' style='position:absolute;left:50px;top:10px;width:100px;height:100px;background:gray;' onclick='console.log("fake click 1");return false;'>Fake click 1</div>
<div href='javascript:void(0);' style='position:absolute;left:50px;top:130px;width:100px;height:100px;background:gray;' onclick='console.log("fake click 2");return false;'>Fake click 2</div>
</body>

Javascript:

var clientX,clientY;   
var fakeCursor = document.getElementById('fakeCursor'); 

var isFakeMouse = false;
document.onclick = function(e){
    if(isFakeMouse){    
        if(document.elementFromPoint(fakeCursor.style.left,fakeCursor.style.top)!=null){
            document.elementFromPoint(fakeCursor.style.left,fakeCursor.style.top).click();
            return false;
        }
        fakeCursor.style.display = 'inline';
        fakeLeft = clientX;
        fakeTop = clientY;
        var mouseLastLeft = -1;
        var mouseLastTop = -1;
        document.onmousemove = function(e){  
            if(mouseLastLeft ===-1 && mouseLastTop ===-1){
                mouseLastLeft = e.clientX;
                mouseLastTop = e.clientY;
                return;
            } 
            fakeCursor.style.left = (parseInt(fakeCursor.style.left) + ((mouseLastLeft - e.clientX)*-1)) + 'px';
            fakeCursor.style.top = (parseInt(fakeCursor.style.top) + ((mouseLastTop - e.clientY)*-1)) + 'px'; 
            mouseLastLeft = e.clientX;
            mouseLastTop = e.clientY;
        }
    }
    else{
        isFakeMouse = true;
        document.body.style.cursor = "none";
        fakeCursor.style.display = 'none';
        fakeCursor.style.left = clientX = e.clientX;
        fakeCursor.style.top = clientY = e.clientY;
        document.onmousemove = null;
    }
} 

在这里,首先单击documentreal mouse 隐藏。当用户再次单击document 时,real mouse 仍将被隐藏,而新的fake mouse(图像)将取而代之。 fake mouse 的位置与用户离开 real mouse 的位置相同。 fake mouse 工作(尝试)像real mouse 一样工作。

对不起,内联 css 和 javascrict

【讨论】:

  • >Flash\n 就在那儿弄丢了。不,Flash 会死,我不打算使用它。感谢您的链接!
  • @Bane,我已经编辑了我的答案并添加了一些代码。你可以试试我的代码 :) 它不推荐,但你至少应该看到它。也许它会给你一个方向去寻找另一种方式