简体   繁体   English

在我使用 html、css 和 js 添加自定义光标后,该光标在链接或按钮上不起作用。我应该如何解决?

[英]After I added made a custom cursor using html,css and js, that cursor is not working on links or button .How should I solve it?

const cursor = document.querySelector('.cursor');

document.addEventListener('mousemove', e => {
  cursor.setAttribute("style","top: "+(e.pageY-10)+"px; left: "+(e.pageX-10)+"px;")
})

document.addEventListener('click' ,() => {
  cursor.classList.add("expand"); 

  setTimeout(() =>{
    cursor.classList.remove("expand");
  }, 500);
}) 

Try adding the following CSS:尝试添加以下 CSS:

.cursor {
  position: absolute;
}

Here's a fiddle: https://jsfiddle.net/bsxj7pyg/这是一个小提琴: https : //jsfiddle.net/bsxj7pyg/

An element with position: absolute;具有position: absolute;的元素position: absolute; is positioned relative to the nearest positioned ancestor.相对于最近的定位祖先定位。 In this case, it would follow the cursor.在这种情况下,它将跟随光标。

Add the style pointer-events: none;添加样式pointer-events: none; to your custom cursor to stop your cursor responding to mouse events such as click (or touch).到您的自定义光标以停止您的光标响应鼠标事件,例如单击(或触摸)。

See working example below:请参阅下面的工作示例:

 const cursor = document.querySelector('.cursor'); document.addEventListener('mousemove', e => { cursor.setAttribute("style","top: "+(e.pageY-10)+"px; left: "+(e.pageX-10)+"px;") }) document.addEventListener('click' ,() => { cursor.classList.add("expand"); setTimeout(() =>{ cursor.classList.remove("expand"); }, 500) })
 * { cursor: none; } .cursor { height: 20px; width: 20px; background: black; border-radius: 100px; position: absolute; pointer-events: none; /* add pointer-events: none */ }
 <div class="cursor"></div> <a href="https://www.example.com">Link</a>

Alternatively, instead of hiding the cursor and using a div to replace it, you can give it a custom image using:或者,您可以使用以下命令为其提供自定义图像,而不是隐藏光标并使用 div 替换它:

* {
  cursor: url('https://i.stack.imgur.com/8ka1W.png'), auto;
}

See example below:请参阅下面的示例:

 * { cursor: url('https://i.stack.imgur.com/8ka1W.png'), auto; }
 <a href="https://www.example.com">Link</a>

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

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