简体   繁体   English

自定义光标,无法点击DOM元素

[英]Custom Cursor, can't click on DOM elements

I was trying to make a circle that is following mouse, aka custom cursor.我试图制作一个跟随鼠标的圆圈,也就是自定义光标。 In fact it's working as expected.事实上,它按预期工作。 But i have one issue.但我有一个问题。 If there is a for example button, under the circle, and i want to click the button through the circle it doesn't work.例如,如果在圆圈下方有一个按钮,并且我想通过圆圈单击该按钮,则它不起作用。 *I can click on element and it works fine, only when the circle is not under mouse yet. *我可以点击元素并且它工作正常,只有当圆圈不在鼠标下时。

Played with z-index and other thing, but there was no proper result, because i want to have that circle visible over hovered element, like it is in the example down below.玩过 z-index 和其他东西,但没有正确的结果,因为我想让那个圆圈在悬停的元素上可见,就像下面的例子一样。

 window.CustomCursor = new(function() { const self = this; const css = ` .customCursor { z-index: 999; width: 22px; height: 22px; border: 1.2px solid #2980b9; border-radius: 50%; position: absolute; transition-duration: 200ms; transition-timing-function: ease-out; } `; //Creating Style const style = document.createElement('style'); style.appendChild(document.createTextNode(css)); document.querySelector('head').appendChild(style); //Creating Cursor const cursor = document.createElement('div'); cursor.className = "customCursor"; document.querySelector('body').appendChild(cursor); //Creating Logic document.addEventListener("mousemove", e => { const {pageX, pageY} = e; cursor.setAttribute(`style`, `top: ${pageY - 11}px; left: ${pageX - 11}px;`); }); document.addEventListener("click", e => { //soon }); });
 body { background: #0f1215; }
 <button onclick="alert('Hi')">Button</button>

Add pointer-events: none;添加pointer-events: none; to the cursor styles.到光标样式。

const css = `
    .customCursor {
        z-index: 999;
        width: 22px;
        height: 22px;
        border: 1.2px solid #2980b9;
        border-radius: 50%;
        position: absolute;
        transition-duration: 200ms;
        transition-timing-function: ease-out;
        pointer-events: none; /* ADD_ME */
    }
`;

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

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