返回
鼠标移动特效锦囊妙计
前端
2023-12-26 21:02:42
鼠标作为计算机交互的重要设备,其移动轨迹往往蕴藏着丰富的用户信息。通过捕捉和分析鼠标移动数据,可以实现各种有趣的交互效果,例如:
-
效果一:跟随鼠标的彩色光斑
将一个彩色光斑跟随鼠标移动,就像一个调皮的小精灵,在屏幕上留下一道道彩色的痕迹。这种效果可以营造出活泼、有趣的氛围,特别适合用于游戏或儿童网站。
body {
background-color: #000;
}
#cursor {
position: absolute;
top: 0;
left: 0;
width: 10px;
height: 10px;
border-radius: 50%;
background-color: #fff;
}
document.addEventListener("mousemove", (e) => {
const cursor = document.getElementById("cursor");
cursor.style.top = e.clientY - 5 + "px";
cursor.style.left = e.clientX - 5 + "px";
});
-
效果二:鼠标划过的涟漪效果
当鼠标划过网页元素时,元素周围会产生一圈圈涟漪效果,就像在平静的水面上投下一块石子。这种效果可以营造出一种轻盈、灵动的氛围,特别适合用于水相关或女性网站。
body {
background-color: #000;
}
.ripple {
position: absolute;
top: 0;
left: 0;
width: 0;
height: 0;
border-radius: 50%;
background-color: #fff;
opacity: 0.5;
}
document.addEventListener("mousemove", (e) => {
const ripple = document.createElement("div");
ripple.classList.add("ripple");
ripple.style.top = e.clientY + "px";
ripple.style.left = e.clientX + "px";
document.body.appendChild(ripple);
setTimeout(() => {
ripple.remove();
}, 1000);
});
-
效果三:鼠标划过的线条效果
当鼠标划过网页元素时,元素周围会产生一条条线条效果,就像用笔在纸上画画一样。这种效果可以营造出一种艺术、创意的氛围,特别适合用于设计或绘画相关网站。
body {
background-color: #000;
}
.line {
position: absolute;
top: 0;
left: 0;
width: 0;
height: 0;
border-radius: 50%;
background-color: #fff;
opacity: 0.5;
}
document.addEventListener("mousemove", (e) => {
const line = document.createElement("div");
line.classList.add("line");
line.style.top = e.clientY + "px";
line.style.left = e.clientX + "px";
document.body.appendChild(line);
setTimeout(() => {
line.remove();
}, 1000);
});
-
效果四:鼠标划过的粒子效果
当鼠标划过网页元素时,元素周围会产生一个个粒子效果,就像在星空下挥舞魔法棒一样。这种效果可以营造出一种梦幻、科幻的氛围,特别适合用于游戏或太空相关网站。
body {
background-color: #000;
}
.particle {
position: absolute;
top: 0;
left: 0;
width: 10px;
height: 10px;
border-radius: 50%;
background-color: #fff;
opacity: 0.5;
}
document.addEventListener("mousemove", (e) => {
const particle = document.createElement("div");
particle.classList.add("particle");
particle.style.top = e.clientY + "px";
particle.style.left = e.clientX + "px";
document.body.appendChild(particle);
setTimeout(() => {
particle.remove();
}, 1000);
});
-
效果五:鼠标划过的文字效果
当鼠标划过网页元素时,元素周围会产生一段段文字效果,就像在纸上写字一样。这种效果可以营造出一种文学、优雅的氛围,特别适合用于博客或小说相关网站。
body {
background-color: #000;
}
.text {
position: absolute;
top: 0;
left: 0;
width: 0;
height: 0;
border-radius: 50%;
background-color: #fff;
opacity: 0.5;
}
document.addEventListener("mousemove", (e) => {
const text = document.createElement("div");
text.classList.add("text");
text.style.top = e.clientY + "px";
text.style.left = e.clientX + "px";
document.body.appendChild(text);
setTimeout(() => {
text.remove();
}, 1000);
});
这些鼠标移动特效可以通过CSS、JavaScript或其他编程语言实现,也可以通过现成的库或插件来实现。希望这些特效能够为您的网页设计带来更多灵感和创意。