javascript - 自定义 Cursor,不能点击 DOM 元素

标签 javascript html css

我试图制作一个跟随鼠标的圆圈,也就是自定义光标。事实上,它按预期工作。但我有一个问题。如果有一个例如按钮,在圆圈下面,我想通过圆圈单击按钮,它不起作用。 *我可以点击元素并且它工作正常,只有当圆圈不在鼠标下时。

玩过 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>

最佳答案

添加 pointer-events: none; 到光标样式。

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 */
    }
`;

关于javascript - 自定义 Cursor,不能点击 DOM 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54602553/

相关文章:

javascript - 我需要能够在 5 列中显示 2 行文本,并且每列文本需要不同,全部基于 1 个下拉列表?

html - 两列布局中第一列的 CSS 省略号

Css 焦点初学者

php - 将其他对象添加到 JSON 编码数组

javascript - 对象上的展开运算符React Redux reducer

javascript - 如何使用 document.querySelectorAll 选择除 anchor (另一个元素内的 anchor )以外的所有标签?

javascript - 如何用变量名替换 document.write 中的元素

html - HTML5 网络应用程序可以访问 Windows 7 平板电脑上的相机吗

css - 如何旋转文字?

html - 如何防止HTML中两个相同的div重叠?