简体   繁体   English

中心定制 Cursor

[英]Center Custom Cursor

I created a custom cursor that follows the mouse.我创建了一个跟随鼠标的自定义 cursor。 I would like to have the custom circle grow larger when hovered over links.当悬停在链接上时,我希望自定义圆圈变大。 The problem that I am having is that when the circle animates to a larger circle, it no longer is centered to the cursor.我遇到的问题是,当圆圈动画成更大的圆圈时,它不再以 cursor 为中心。 I'm not too sure how to keep the custom circle centered as it grows in size.我不太确定如何在自定义圆圈变大时保持其居中。 The code below is the JS used to have the custom circle follow the cursor.下面的代码是用来让自定义圆跟随cursor的JS。 Thank you in advance!先感谢您!

 let clientX = -100; let clientY = -100; const innerCursor = document.querySelector(".cursor--small"); const initCursor = () => { document.addEventListener("mousemove", e => { clientX = e.clientX; clientY = e.clientY; }); const render = () => { innerCursor.style.transform = `translate(${clientX}px, ${clientY}px)`; requestAnimationFrame(render); }; requestAnimationFrame(render); }; initCursor();

It's not possible to programmatically move the user's cursor.无法以编程方式移动用户的 cursor。 You can lock it into a position, but that would create a bad UX and is probably problematic in other ways (cross browser issues).您可以将其锁定在 position 中,但这会产生糟糕的用户体验,并且可能在其他方面存在问题(跨浏览器问题)。

https://stackoverflow.com/a/19870963/1772933 https://stackoverflow.com/a/19870963/1772933

I would suggest creating a larger 'hit' area for the hover using styles.我建议使用 styles 为 hover 创建一个更大的“命中”区域。

 a.cursor-thing{ display:inline-block; padding:20px; margin:-20px; } a.cursor-thing:hover{ background:#f00; }
 Here is a sentence where you can <a href='#' class='cursor-thing'>hover over me</a> or even <a href='#' class='cursor-thing'>hover over me</a>.

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

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