效果展示

CSS / JavaScript 知识点
- background-image 绘制网格背景
- filter 属性的运用
- onmousemove 事件运用
- getBoundingClientRect 方法的运用
实现页面基础结构
<div class="cursour">div>
实现网格背景样式
body {
min-height: 100vh;
overflow: hidden;
background: #222;
background-image: linear-gradient(to right, #333 1px, transparent 1px),
linear-gradient(to bottom, #333 1px, transparent 1px);
background-size: 40px 40px;
cursor: none;
}
实现自定义光标
.cursour {
position: fixed;
width: 25px;
height: 25px;
border-top: 5px solid #0f0;
border-left: 5px solid #0f0;
transform-origin: top;
transform: translate(-1px, 5px) rotate(15deg) scale(0);
transition: transform 0.1s;
pointer-events: none;
filter: drop-shadow(0 0 5px #0f0) drop-shadow(0 0 15px #0f0) drop-shadow(
0 0 35px #0f0
) hue-rotate(60deg);
}
.cursour::before {
content: "";
position: absolute;
left: -2.5px;
width: 5px;
height: 40px;
background: #0f0;
transform-origin: top;
transform: rotate(315deg);
}
- 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
实现移动光标移动并随机产生字母
let cursour = document.querySelector(".cursour");
let body = document.querySelector("body");
document.onmousemove = function (e) {
cursour.style.top = e.pageY + "px";
cursour.style.left = e.pageX + "px";
body.style.backgroundPositionX = e.pageX / -4 + "px";
body.style.backgroundPositionY = e.pageY / -4 + "px";
let element = document.createElement("div");
element.className = "element";
body.prepend(element);
element.style.left = cursour.getBoundingClientRect().x + "px";
element.style.top = cursour.getBoundingClientRect().y - 20 + "px";
setTimeout(() => {
let text = document.querySelectorAll(".element")[0];
directionX = Math.random() < 0.5 ? -1 : 1;
directionY = Math.random() < 0.5 ? -1 : 1;
text.style.left =
parseInt(text.style.left) - directionX * (Math.random() * 200) + "px";
text.style.top =
parseInt(text.style.top) - directionX * (Math.random() * 200) + "px";
text.style.opacity = 0;
text.style.transform = "scale(0.25)";
text.innerHTML = randomText();
setTimeout(() => {
element.remove();
}, 1000);
}, 10);
};
function randomText() {
const text = "abcdefghijklmnopqrstuvwxyz1234567890".split("");
return text[parseInt(Math.random() * text.length)];
}

- 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
编写随机生成出的字母样式
.element {
position: absolute;
transform: translate(-50%, -50%);
color: #0f0;
pointer-events: none;
width: 10px;
height: 10px;
transition: 1s;
font-size: 2em;
filter: drop-shadow(0 0 5px #0f0) drop-shadow(0 0 25px #0f0) hue-rotate(60deg);
}
完整代码下载
完整代码下载