简体   繁体   English

鼠标指针悬停在按钮中心

[英]Mouse pointer at center of button on hover

I have a set of buttons shown on the screen. 屏幕上显示了一组按钮。 I am trying to implement a function which executes upon hovering on a button (having a class 'button'). 我正在尝试实现一个功能,该功能在将鼠标悬停在按钮上(具有类“按钮”)时执行。

This function needs to display the mouse pointer at the center of the button, meaning that whilst the actual mouse pointer is on the button, the mouse pointer is displayed in the center of the button. 此功能需要将鼠标指针显示在按钮的中心,这意味着当实际的鼠标指针位于按钮上时,鼠标指针显示在按钮的中心。 I tried using the RequestPointerLock API but this hides the mouse pointer whereas I want it to be displayed and I believe it only works with the event handler onclick . 我尝试使用RequestPointerLock API,但这会隐藏鼠标指针,而我希望它能够显示,并且我相信它只能与事件处理程序onclick

This is what I've done so far: 到目前为止,这是我所做的:

var buttons = document.getElementsByClassName('button');
buttons.requestPointerLock = buttons.requestPointerLock || buttons.mozRequestPointerLock;
document.exitPointerLock = document.exitPointerLock || document.mozExitPointerLock;
document.addEventListener('pointerlockchange', lockChangeAlert, false);
document.addEventListener('mozpointerlockchange', lockChangeAlert, false);

function lockChangeAlert() 
{
    if (document.pointerLockElement === buttons || document.mozPointerLockElement === buttons) 
    {
        console.log('The pointer lock status is now locked');
        document.addEventListener("mousemove", updatePosition, false);
    } else 
    {
        console.log('The pointer lock status is now unlocked');  
        document.removeEventListener("mousemove", updatePosition, false);
    }
}

function updatePosition(e) 
{
}

buttons.onclick = function()
{
     buttons.requestPointerLock();
}

Any ideas on how I can implement this please? 请问我如何实施此方法的任何想法?

There is no way for you to set the position of the cursor using JavaScript. 没有办法让你使用JavaScript设置光标的位置。 This goes against a number of security considerations . 这违背了许多安全考虑 The best you can do is hide the cursor (with cursor: none on :hover , and draw a static image of one on the center of the button. 最好的办法是隐藏光标(在:hover上使用cursor: none ,并在按钮中心绘制一个静态图片。

Please note that the inconsistency of the cursor icons on different systems will prove to be a problem if you choose to go down this path. 请注意,如果您选择沿着这条路径浏览,则在不同系统上光标图标的不一致将被证明是一个问题。

As it is with a few other JavaScript APIs, requestPointerLock doesn't work with events such as mouseover (not unlike requestFullscreen ). 与其他一些JavaScript API一样, requestPointerLock不适用于mouseover事件(与requestFullscreen不同)。 They need direct user interaction to work. 他们需要直接的用户交互才能工作。 An event such as mouseover would be exploited too easily. mouseover的事件将很容易被利用。

The Pointer Lock API example uses a canvas to draw a circle to a canvas. 指针锁定API示例使用画布在画布上绘制圆。

This might be helpful. 这可能会有所帮助。

 button { background: #333; position: relative; color: #fff; padding: 40px 80px; border: none; font-size: 32px; } button:hover { cursor: none; } button:hover:after { content: ''; background-image: url("http://www.freeiconspng.com/uploads/original-file---svg-file-nominally-550--400-pixels-file-size-3--23.png"); width: 48px; height: 48px; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); background-size: contain; background-repeat: no-repeat; } 
 <button>Here</button> 

Here is also the codepen: http://codepen.io/hunzaboy/pen/pNwOqZ 这也是codepen: http ://codepen.io/hunzaboy/pen/pNwOqZ

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM