使用 A-frame 实现鼠标指针显示功能的方法。

8

我目前正在使用A-frame (https://aframe.io)场景,其中我隐藏了场景中的鼠标指针。如何创建一个功能,使得当发出“showPointer”函数时,我的鼠标指针会显示,当发生另一个函数时,“hidePointer”函数,我的鼠标指针将再次隐藏。

当前默认情况下,我的鼠标指针被隐藏。我希望当调用“showPointer”函数时,我的鼠标指针将再次显示,并且当调用“hidePointer”函数时,我的鼠标指针将再次隐藏。我应该怎么做?我的函数:

<script>
function hidePointer() {
//hide mouse pointer
}

function showPointer() {
//show mouse pointer 
} 
</script>

1
请参阅此问题:https://dev59.com/enNA5IYBdhLWcg3wKaYx - JosephDaSilva
我猜测这个问题的代码与那个问题完全不同,因为我正在尝试在渲染为3D的WebVR环境中显示或隐藏光标,而不是2D网页。 - Aidan Young
look-controls="pointerLockEnabled: true;" 有帮助吗? - Lajos Arpad
并不是因为我想保持指针锁定在隐藏或非隐藏的固定状态,而是我希望能够根据函数显示或隐藏指针,以便它不处于固定状态。 - Aidan Young
JavaScript本身无法隐藏您的光标,这是一种样式。 - JavaScript
显示剩余2条评论
2个回答

0
<script>
function hidePointer() {
  $('a-scene').canvas.style.cursor='none'
}
            
function showPointer() {
  $('a-scene').canvas.style.cursor='pointer'
  // replace "pointer" with other style keyword
} 
</script>

关于光标样式的更多细节,请查看此处

请确保从 canvas 元素中删除 a-grab-cursor 类

使用以下代码删除:$('a-frame').classList.remove("a-grab-cursor")

详见此处

如果您使用“cursor”组件,请禁用启用鼠标光标样式


0

const fullBrowserWindow = document.querySelector(`body`);
const popupElement = document.querySelector(`div.popup`);

function hidePointer() {
  fullBrowserWindow.style.cursor = 'none';
}

function showPointer() {
  fullBrowserWindow.style.cursor = 'default';
}

popupElement.onmouseenter = (event) => {
  showPointer();
  console.log('Mouse entered the div, Pointer Shown!');
};

popupElement.onmouseleave = (event) => {
  hidePointer();
  console.log('Mouse left the div, Pointer Removed!');
};
body {
  width: 100%;
  height: 500px;
  padding: 0;
  margin: 0;
  cursor: none;
  background-color: #ff0000;
}
body div.wegbl {
  width: 480px;
  height: 312px;
  background-color: #000000;
}
body div.popup {
  width: 200px;
  height: 35px;
  background-color: #ffffff;
  position: absolute;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>No Cursor - on when mouse is over popup</title>
</head>
<body>
    <div class="webgl"></div>
    <div class="popup">Popup Promt? [Y/N]</div>
</body>
</html>


代码运行完美,但使用CSS隐藏光标并且在函数中无法隐藏光标。我需要使用JavaScript函数来隐藏光标。当函数发生时,光标应该被隐藏,而当另一个函数发生时,光标应该再次显示。 - Aidan Young
据我所知,Javascript没有隐藏光标的API,而是通过CSS属性cursor设置为“none”来实现。即使在A-frame中,当您应用look-controls =“pointerLockEnabled:true;”时,它们也使用set canvasElement.style.cursor ='none'。 - Ibrahim W.
我正在尝试相反的操作,我想在任何时候隐藏我的光标,但当场景中的对象被点击时会触发2D弹出窗口。我想知道如何将我的光标隐藏为默认值,这样当我悬停在带有Popup1类的div弹出窗口上时,光标应该显示。 - Aidan Young
请检查一下解决方案,我已经修改了它。我想始终隐藏我的光标 => 在HTML页面的body元素上使用cursor: none; CSS属性(这意味着光标始终处于隐藏状态)=> 弹出窗口可以是一个具有position: absolute;的div,当鼠标进入/离开该div时,您可以显示/隐藏光标,请检查修改后的代码片段。谢谢! - Ibrahim W.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接