之前看到有網站的游標長得像這樣,就一直很想用。這次花了些時間研究給弄好了。
游標

這個原理其實很簡單,用兩個 div,border-radius: 50%; 呈現圓形。CSS 套用樣式,JavaScript 讓他跟隨滑鼠就完成。

在程式和樣式部分我參考了這個:

See the Pen Circle Follow Cursor by Hakadao (@Hakadao) on CodePen.

這是我修改後的程式與樣式,讓他更搭配我的主題,也另外增加了一些顏色與形狀的變化:

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
const cursor = document.querySelector("#cursor");
const cursorBorder = document.querySelector("#cursor-border");
const links = document.querySelectorAll("a");
const texts = document.querySelectorAll("p");
const cursorPos = { x: 0, y: 0 };
const cursorBorderPos = { x: 0, y: 0 };

cursor.style.setProperty("--width", "10px");
cursor.style.setProperty("--height", "10px");

document.addEventListener("mousemove", (e) => {
cursorPos.x = e.clientX;
cursorPos.y = e.clientY;

cursor.style.transform = `translate(${e.clientX}px, ${e.clientY}px)`;
});

document.addEventListener("mousedown", (e) => {
cursor.style.setProperty("--width", `${cursor.style.getPropertyValue("--width").replace("px", "") * 1.8}px`);
cursor.style.setProperty("--height", `${cursor.style.getPropertyValue("--height").replace("px", "") * 1.8}px`);
});

document.addEventListener("mouseup", (e) => {
cursor.style.setProperty("--width", `${cursor.style.getPropertyValue("--width").replace("px", "") / 1.8}px`);
cursor.style.setProperty("--height", `${cursor.style.getPropertyValue("--height").replace("px", "") / 1.8}px`);
});

texts.forEach((text) => {
text.addEventListener("mouseover", (e) => {
cursor.style.borderRadius = "0%";
cursor.style.setProperty("--width", "2px");
cursor.style.setProperty("--height", "20px");
});
text.addEventListener("mouseout", (e) => {
cursor.style.borderRadius = "50%";
cursor.style.setProperty("--width", "10px");
cursor.style.setProperty("--height", "10px");
});
});

links.forEach((link) => {
link.addEventListener("mouseover", (e) => {
cursor.style.backgroundColor = "#daaf61";
cursorBorder.style.setProperty("--size", "50px");
});
link.addEventListener("mouseout", (e) => {
cursor.style.backgroundColor = "#95bcd6";
cursorBorder.style.setProperty("--size", "28px");
});
});

requestAnimationFrame(function loop() {
const easting = 8;
cursorBorderPos.x += (cursorPos.x - cursorBorderPos.x) / easting;
cursorBorderPos.y += (cursorPos.y - cursorBorderPos.y) / easting;

cursorBorder.style.transform = `translate(${cursorBorderPos.x}px, ${cursorBorderPos.y}px)`;
requestAnimationFrame(loop);
});
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
* {
cursor: none;
}

/* 這段是專門對 butterfly 的,其他主題可能要自行修改 */
.search-dialog, .fancybox__container {
*{cursor: auto;}
}

#cursor {
position: fixed;
top: calc(var(--height) / -2);
left: calc(var(--width) / -2);
width: var(--width);
height: var(--height);
background-color: #95bcd6;
transition: top 0.15s ease-out, left 0.15s ease-out, width 0.15s ease-out, height 0.15s ease-out, background-color 0.15s ease-out;
border-radius: 50%;
pointer-events: none;
z-index: 999;
}

#cursor-border {
--size: 28px;
position: fixed;
top: calc(var(--size) / -2);
left: calc(var(--size) / -2);
width: var(--size);
height: var(--size);
border-radius: 50%;
background-color: #5c565156;
mix-blend-mode: difference;
pointer-events: none;
transition: top 0.15s ease-out, left 0.15s ease-out, width 0.15s ease-out, height 0.15s ease-out, background-color 0.15s ease-out;
z-index: 999;
}

把以上兩個檔案存在 source 資料夾下,並且在設定文件中的 Inject 加入下面這行就可以,記得路徑要填對。

1
<div id="cursor-border"></div><div id="cursor"></div><link rel="stylesheet" href="/css/custom_cursor.css"><script src="/js/custom_cursor.js"></script>

當然其他不是 butterfly,而是其他網站或主題也可以用。

q(≧▽≦q)