点击涟漪
Click Ripple
圆形高光从按下的位置向外扩散,并在控件内淡出。
适用场景
让按钮或触屏控件在点按位置即时给出反馈。 让波纹留在被按下的控件里,触屏上相邻按钮挨得近时尤其容易看出刚才点中了哪一个。
应用示例
在手机点餐页点一道菜旁的「加入」,波纹从指尖落下的位置向外扩散,在按钮内淡去,底部同时更新点餐金额。
也叫
- 点按水波纹
- 点击波纹
AI 提示词
给【目标控件】加上 Click Ripple。以点击点在控件内的位置作为圆心,扩大圆形高光直至覆盖区域并淡出,按现有圆角裁切。键盘触发用中心点,保留按钮原有操作,减少动态效果模式下改用短暂的颜色反馈。
示例代码
保存为 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;
}
.ripple-button {
position: relative;
isolation: isolate;
min-width: 132px;
padding: 14px 19px;
overflow: hidden;
color: #271a37;
background: #d5c5ff;
border: 1px solid #eccffb;
border-radius: 999px;
box-shadow: 0 0 0 7px #b89af820, 0 0 0 15px #b89af811;
font: inherit;
font-size: 13px;
font-weight: 750;
cursor: pointer;
}
.ripple-button:active { background: #eee3ff; }
.ripple-label { position: relative; z-index: 1; }
.ripple-wave {
position: absolute;
width: var(--diameter);
height: var(--diameter);
left: var(--origin-x);
top: var(--origin-y);
border-radius: 50%;
background: #fff;
animation: expand-ripple 520ms ease-out forwards;
pointer-events: none;
}
@keyframes expand-ripple {
from { transform: translate(-50%, -50%) scale(0); opacity: 0.8; }
to { transform: translate(-50%, -50%) scale(1); opacity: 0; }
}
@media (prefers-reduced-motion: reduce) {
.ripple-wave { animation: none; display: none; }
}
</style>
</head>
<body>
<main class="demo">
<button class="ripple-button" type="button">
<span class="ripple-label">点一下</span>
</button>
</main>
<script>
const button = document.querySelector(".ripple-button");
const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)");
button.addEventListener("click", (event) => {
button.querySelector(".ripple-wave")?.remove();
if (reduceMotion.matches) return;
const { left, top, width, height } = button.getBoundingClientRect();
const x = event.detail === 0 ? width / 2 : event.clientX - left;
const y = event.detail === 0 ? height / 2 : event.clientY - top;
const maxX = Math.max(x, width - x);
const maxY = Math.max(y, height - y);
const wave = document.createElement("span");
wave.className = "ripple-wave";
wave.setAttribute("aria-hidden", "true");
wave.style.setProperty("--origin-x", x + "px");
wave.style.setProperty("--origin-y", y + "px");
wave.style.setProperty("--diameter", (2 * Math.hypot(maxX, maxY)) + "px");
wave.addEventListener("animationend", () => wave.remove(), { once: true });
button.append(wave);
});
</script>
</body>
</html>UI Skills 原创示例