1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| const cursor = document.querySelector("#cursor"); const cursorBorder = document.querySelector("#cursor-border");
if (!cursor || !cursorBorder) { console.warn("Custom cursor elements not found."); }
const cursorPos = { x: 0, y: 0 }; const cursorBorderPos = { x: 0, y: 0 }; let currentState = "default";
cursor.style.setProperty("--width", "10px"); cursor.style.setProperty("--height", "10px");
function setDefaultCursor() { if (currentState === "default") return; currentState = "default";
cursor.style.borderRadius = "50%"; cursor.style.setProperty("--width", "10px"); cursor.style.setProperty("--height", "10px"); cursor.style.backgroundColor = "#95bcd6"; cursorBorder.style.setProperty("--size", "28px"); }
function setTextCursor() { if (currentState === "text") return; currentState = "text";
cursor.style.borderRadius = "0%"; cursor.style.setProperty("--width", "2px"); cursor.style.setProperty("--height", "20px"); cursor.style.backgroundColor = "#95bcd6"; cursorBorder.style.setProperty("--size", "28px"); }
function setInteractiveCursor() { if (currentState === "interactive") return; currentState = "interactive";
cursor.style.borderRadius = "50%"; cursor.style.setProperty("--width", "10px"); cursor.style.setProperty("--height", "10px"); cursor.style.backgroundColor = "#daaf61"; cursorBorder.style.setProperty("--size", "50px"); }
document.addEventListener("mousemove", (e) => { const x = e.clientX; const y = e.clientY;
cursorPos.x = x; cursorPos.y = y;
cursor.style.transform = `translate3d(${x}px, ${y}px, 0)`;
const el = document.elementFromPoint(x, y); if (!el) return;
if (el.closest("a, button, [role='button'], input[type='button'], input[type='submit']")) { setInteractiveCursor(); return; }
const textEl = el.closest( "p, span, h1, h2, h3, h4, h5, h6, li, strong, em, small, label" );
if (textEl && textEl.textContent.trim() !== "") { setTextCursor(); } else { setDefaultCursor(); } });
document.addEventListener("mousedown", () => { const w = parseFloat(getComputedStyle(cursor).getPropertyValue("--width")) || 10; const h = parseFloat(getComputedStyle(cursor).getPropertyValue("--height")) || 10;
cursor.style.setProperty("--width", `${w * 1.8}px`); cursor.style.setProperty("--height", `${h * 1.8}px`); });
document.addEventListener("mouseup", () => { const w = parseFloat(getComputedStyle(cursor).getPropertyValue("--width")) || 10; const h = parseFloat(getComputedStyle(cursor).getPropertyValue("--height")) || 10;
cursor.style.setProperty("--width", `${w / 1.8}px`); cursor.style.setProperty("--height", `${h / 1.8}px`); });
(function loop() { const easing = 8;
cursorBorderPos.x += (cursorPos.x - cursorBorderPos.x) / easing; cursorBorderPos.y += (cursorPos.y - cursorBorderPos.y) / easing;
cursorBorder.style.transform = `translate3d(${cursorBorderPos.x}px, ${cursorBorderPos.y}px, 0)`;
requestAnimationFrame(loop); })();
|