液态光标
Blob Cursor
柔边液滴略带延迟地跟随指针,靠近控件时拉伸并吸附。
适用场景
用于创意作品集或工作室网站的自定义指针反馈。 把跟随液滴作为装饰,保留系统光标与清晰可见的键盘焦点。
应用示例
设计工作室在作品入口放一个柔软的蓝色液滴,指针靠近项目按钮时,液滴会向按钮伸展。
也叫
- 液滴光标
- 流体光标
AI 提示词
给【区块】加上液态光标。柔软的液滴略带延迟地跟随指针,靠近可点击元素时拉伸。保留原生光标。触屏和减少动态效果模式下隐藏液滴。
示例代码
保存为 HTML 文件后,用浏览器打开即可运行。代码与页面里的演示一致。
查看 HTML
<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>液态光标</title>
<style>
:root {
color-scheme: dark;
--demo-bg: #171629;
--demo-ink: #f8f5ff;
--demo-accent: #b9a6ff;
--demo-muted: #bfbbd3;
font-family: system-ui, sans-serif;
color: var(--demo-ink);
background: var(--demo-bg);
}
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; min-width: 0; }
button, input { font: inherit; }
.demo {
display: grid;
place-items: center;
align-content: center;
min-height: 100vh;
padding: 14px;
overflow: hidden;
background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
}
.demo-button {
border: 1px solid #ffffff3d;
border-radius: 7px;
background: #ffffff12;
color: var(--demo-ink);
padding: 6px 11px;
font-size: 12px;
cursor: pointer;
}
:focus-visible {
outline: 2px solid var(--demo-accent);
outline-offset: 3px;
}
.demo { background: #141e28; }
.cursor-scene {
position: relative;
isolation: isolate;
display: flex;
justify-content: space-between;
align-items: center;
overflow: hidden;
width: min(100%, 300px);
height: min(66vh, 154px);
padding: 21px;
border: 1px solid #90aec239;
border-radius: 16px;
background:
radial-gradient(circle, #879fb979 1px, transparent 1.2px) 50% 50% / 16px 16px,
linear-gradient(120deg, #182a38, #0c1c28);
}
.cursor-target {
z-index: 1;
display: grid;
place-items: center;
width: 43px;
height: 43px;
padding: 0;
border: 1px solid #a9c1d77a;
border-radius: 13px;
background: #132636d4;
color: #d3efd6;
cursor: pointer;
}
.cursor-target[aria-pressed='true'] { color: #1c3733; background: #c1f4bd; border-color: #c1f4bd; }
.cursor-target > span { width: 12px; aspect-ratio: 1; border: 1.5px solid currentColor; }
[data-shape='circle'] > span { border-radius: 50%; }
[data-shape='diamond'] > span { transform: rotate(45deg); }
.blob-sample, .cursor-blob {
position: absolute;
top: 0;
left: 0;
width: 50px;
aspect-ratio: 1;
border-radius: 58% 42% 46% 54% / 50% 56% 44% 50%;
background: radial-gradient(circle at 29% 24%, #edffd4, #9decae 42%, #47b9a2 81%);
box-shadow: inset -4px -4px 9px #448d78, 0 3px 15px #82e2b15c;
pointer-events: none;
transition: opacity .22s ease;
}
.blob-sample { top: 46%; left: 52%; transform: translate(-50%, -50%) rotate(-29deg) scale(1.3, .9); }
.cursor-blob { opacity: 0; }
.cursor-scene[data-following] .blob-sample { opacity: 0; }
.cursor-scene[data-following] .cursor-blob { opacity: 1; }
@media (prefers-reduced-motion: reduce), (hover: none), (pointer: coarse) {
.blob-sample { transition: none; }
.cursor-blob { display: none; }
}
</style>
</head>
<body>
<main class="demo">
<div class="cursor-scene">
<span class="blob-sample" aria-hidden="true"></span>
<span class="cursor-blob" aria-hidden="true"></span>
<button class="cursor-target" type="button" aria-pressed="false" aria-label="选中圆形" data-shape="circle"><span aria-hidden="true"></span></button>
<button class="cursor-target" type="button" aria-pressed="false" aria-label="选中菱形" data-shape="diamond"><span aria-hidden="true"></span></button>
</div>
</main>
<script>
const scene = document.querySelector('.cursor-scene');
const blob = document.querySelector('.cursor-blob');
const targets = document.querySelectorAll('.cursor-target');
const disable = matchMedia('(prefers-reduced-motion: reduce), (hover: none), (pointer: coarse)');
let x = 0;
let y = 0;
let targetX = 0;
let targetY = 0;
let magnet = false;
let stretch = 1;
let angle = 0;
let frame = 0;
function follow() {
frame = 0;
const dx = targetX - x;
const dy = targetY - y;
const distance = Math.hypot(dx, dy);
x += dx * .2;
y += dy * .2;
if (distance > 1) angle = Math.atan2(dy, dx);
const desiredStretch = magnet ? 1.48 : 1 + Math.min(.57, distance / 90);
stretch += (desiredStretch - stretch) * .23;
blob.style.transform = 'translate(' + (x - 25) + 'px, ' + (y - 25) + 'px) rotate(' + angle + 'rad) scale(' + stretch + ', ' + (1 / Math.sqrt(stretch)) + ')';
if (distance > .3 || Math.abs(stretch - desiredStretch) > .01) frame = requestAnimationFrame(follow);
}
scene.addEventListener('pointermove', (event) => {
if (disable.matches || event.pointerType === 'touch') return;
const bounds = scene.getBoundingClientRect();
const target = event.target.closest('.cursor-target');
const buttonBounds = target?.getBoundingClientRect();
targetX = (buttonBounds ? (event.clientX + 2 * (buttonBounds.left + buttonBounds.width / 2)) / 3 : event.clientX) - bounds.left;
targetY = (buttonBounds ? (event.clientY + 2 * (buttonBounds.top + buttonBounds.height / 2)) / 3 : event.clientY) - bounds.top;
magnet = Boolean(target);
if (!scene.hasAttribute('data-following')) {
x = targetX;
y = targetY;
scene.setAttribute('data-following', '');
}
if (!frame) frame = requestAnimationFrame(follow);
});
function reset() {
scene.removeAttribute('data-following');
cancelAnimationFrame(frame);
frame = 0;
stretch = 1;
}
scene.addEventListener('pointerleave', reset);
disable.addEventListener('change', reset);
for (const target of targets) {
target.addEventListener('click', () => {
const pressed = target.getAttribute('aria-pressed') !== 'true';
for (const other of targets) other.setAttribute('aria-pressed', String(other === target && pressed));
});
}
</script>
</body>
</html>UI Skills 原创示例