05A Popover That Follows by Inertia and Resists Like Rubber at the Edge
While the anchor is dragged the popover trails by inertial interpolation, and distance past a screen edge is scaled down by a resistance factor so it stretches like rubber.
Best forMovable note cards
FOLLOW_LERPRUBBEREDGE_PADLAG

<main class="stage">
<div class="pm is-demo" id="pm" data-demo="1">
<p class="pm__live" data-r="live" aria-live="off" aria-atomic="true"></p>
<i class="pm__bound" data-r="bound"></i>
<i class="pm__tether" data-r="tether"></i>
<div class="pm__pop" data-r="pop"><b class="pm__ttl">Note</b><span class="pm__txt">Drag to move</span></div>
<button class="pm__anchor" data-r="anchor" type="button" aria-label="Pin, move with arrow keys"></button>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
// 핵심: 허용 범위를 넘은 거리는 저항 계수로 줄이고 상한(LAG)에서 포화시켜, 앵커와 팝오버 사이 끈이 고무줄처럼 늘어난다.
@include stage($stage-ink);
.pm {
position: relative;
box-sizing: border-box;
overflow: hidden;
width: min(100%, 440px);
height: min(252px, calc(100vh - 2 * clamp(8px, 4vw, 24px)));
border-radius: $r-card;
background: rgba($subject-cream, .07);
color: $subject-cream;
font-size: 12px;
line-height: 16px;
font-weight: 600;
user-select: none;
}
.pm b { font-weight: 800; font-variant-numeric: tabular-nums; }
.pm__live {
position: absolute;
left: $sp-3;
top: $sp-2;
margin: 0;
z-index: 9;
color: $tick-mute;
font-variant-numeric: tabular-nums;
white-space: nowrap;
pointer-events: none;
}
.pm button { font-family: inherit; }
.pm button:focus-visible { outline: 3px solid $subject-pink; outline-offset: 2px; }
.pm { touch-action: none; }
.pm__bound {
position: absolute;
left: 0;
top: 0;
box-sizing: border-box;
border: 2px dashed rgba($subject-cream, .26);
border-radius: $r-control;
pointer-events: none;
}
.pm__tether {
position: absolute;
left: 0;
top: 0;
width: 1px;
height: 2px;
transform-origin: 0 50%;
background: $subject-pink;
opacity: .8;
pointer-events: none;
}
.pm__anchor {
position: absolute;
left: 0;
top: 0;
box-sizing: border-box;
width: 28px;
height: 28px;
padding: 0;
border: 0;
border-radius: 50%;
background: $subject-pink;
box-shadow: 0 0 0 4px rgba($subject-pink, .3), $shadow-press;
cursor: grab;
z-index: 5;
}
.pm__pop {
position: absolute;
left: 0;
top: 0;
box-sizing: border-box;
width: 116px;
height: 56px;
padding: $sp-2 $sp-3;
border-radius: $r-control;
background: $subject-cream;
color: $subject-ink;
box-shadow: $shadow-raised;
z-index: 4;
}
.pm__ttl { display: block; font-size: 13px; line-height: 20px; }
.pm__txt { display: block; color: rgba($subject-ink, .72); white-space: nowrap; }
@media (prefers-reduced-motion: reduce) {
.pm__anchor { cursor: default; }
}html, body {
margin: 0;
overflow: hidden;
}
.stage {
box-sizing: border-box;
min-height: 100vh;
display: grid;
place-items: center;
margin: 0;
background: #17141a;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.pm {
position: relative;
box-sizing: border-box;
overflow: hidden;
width: min(100%, 440px);
height: min(252px, 100vh - 2 * clamp(8px, 4vw, 24px));
border-radius: 16px;
background: rgba(255, 247, 230, 0.07);
color: #fff7e6;
font-size: 12px;
line-height: 16px;
font-weight: 600;
user-select: none;
}
.pm b {
font-weight: 800;
font-variant-numeric: tabular-nums;
}
.pm__live {
position: absolute;
left: 12px;
top: 8px;
margin: 0;
z-index: 9;
color: #9a93a6;
font-variant-numeric: tabular-nums;
white-space: nowrap;
pointer-events: none;
}
.pm button {
font-family: inherit;
}
.pm button:focus-visible {
outline: 3px solid #ff9ebb;
outline-offset: 2px;
}
.pm {
touch-action: none;
}
.pm__bound {
position: absolute;
left: 0;
top: 0;
box-sizing: border-box;
border: 2px dashed rgba(255, 247, 230, 0.26);
border-radius: 12px;
pointer-events: none;
}
.pm__tether {
position: absolute;
left: 0;
top: 0;
width: 1px;
height: 2px;
transform-origin: 0 50%;
background: #ff9ebb;
opacity: 0.8;
pointer-events: none;
}
.pm__anchor {
position: absolute;
left: 0;
top: 0;
box-sizing: border-box;
width: 28px;
height: 28px;
padding: 0;
border: 0;
border-radius: 50%;
background: #ff9ebb;
box-shadow: 0 0 0 4px rgba(255, 158, 187, 0.3), 0 6px 12px rgba(23, 20, 26, 0.32);
cursor: grab;
z-index: 5;
}
.pm__pop {
position: absolute;
left: 0;
top: 0;
box-sizing: border-box;
width: 116px;
height: 56px;
padding: 8px 12px;
border-radius: 12px;
background: #fff7e6;
color: #17141a;
box-shadow: 0 14px 24px rgba(23, 20, 26, 0.32);
z-index: 4;
}
.pm__ttl {
display: block;
font-size: 13px;
line-height: 20px;
}
.pm__txt {
display: block;
color: rgba(23, 20, 26, 0.72);
white-space: nowrap;
}
@media (prefers-reduced-motion: reduce) {
.pm__anchor {
cursor: default;
}
}(function () {
const root = document.getElementById("pm");
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let demo = !reduce;
let raf = 0;
let t0 = -1;
let last = -1;
let acc = 0;
let clock = 0;
const offs = [];
const live = root.querySelector("[data-r=live]");
function q(sel) {
return root.querySelector(sel);
}
function clamp(v, lo, hi) {
return Math.max(lo, Math.min(hi, v));
}
function seg(t, a, b) {
const c = clamp((t - a) / (b - a), 0, 1);
return c * c * (3 - 2 * c);
}
function on(el, type, fn) {
el.addEventListener(type, fn);
offs.push(() => el.removeEventListener(type, fn));
}
function stopDemo() {
if (!demo)
return;
demo = false;
root.removeAttribute("data-demo");
root.classList.remove("is-demo");
live.setAttribute("aria-live", "polite");
}
if (reduce)
live.setAttribute("aria-live", "polite");
let shown = "";
function say(s) {
if (s === shown)
return;
shown = s;
live.textContent = s;
}
function makeRng(seed) {
let s = seed >>> 0;
return () => {
s = (Math.imul(s, 1664525) + 1013904223) >>> 0;
return s / 4294967296;
};
}
function local(e) {
const r = root.getBoundingClientRect();
return { x: e.clientX - r.left, y: e.clientY - r.top };
}
function sync() {
if (reduce) {
snap();
render();
}
}
// 기법: 팝오버 목표 위치가 허용 범위를 넘으면 넘은 거리에 저항(RUBBER)을 곱하고 LAG 에서 포화시킨 뒤, FOLLOW_LERP 관성으로 쫓아간다.
const FOLLOW_LERP = 0.14;
const RUBBER = 0.55;
const EDGE_PAD = 20;
const LAG = 28;
const POP_W = 116;
const POP_H = 56;
const ANCHOR_R = 14;
const LIFT = 10;
const SWEEP_MS = 2600;
const anchor = q("[data-r=anchor]");
const pop = q("[data-r=pop]");
const tether = q("[data-r=tether]");
const bound = q("[data-r=bound]");
let nx = reduce ? 0.94 : 0.5;
let ny = reduce ? 0.2 : 0.55;
let px = 0;
let py = 0;
let placed = false;
let dragging = false;
let overX = 0;
let overY = 0;
function rubber(over) {
const o = Math.abs(over);
const r = LAG * (1 - 1 / (1 + (o * RUBBER) / LAG));
return over < 0 ? -r : r;
}
function resist(v, lo, hi) {
if (v < lo)
return lo + rubber(v - lo);
if (v > hi)
return hi + rubber(v - hi);
return v;
}
function goal() {
const w = root.clientWidth;
const h = root.clientHeight;
const tx = nx * w - POP_W / 2;
const ty = ny * h - ANCHOR_R - LIFT - POP_H;
const lx = EDGE_PAD;
const hx = w - EDGE_PAD - POP_W;
const ly = EDGE_PAD;
const hy = h - EDGE_PAD - POP_H;
overX = tx < lx ? tx - lx : tx > hx ? tx - hx : 0;
overY = ty < ly ? ty - ly : ty > hy ? ty - hy : 0;
return [resist(tx, lx, hx), resist(ty, ly, hy)];
}
function tick(ms) {
if (demo) {
const t = Math.max(0, ms - 200);
nx = clamp(0.5 + 0.58 * Math.sin((t / SWEEP_MS) * Math.PI * 2), 0.06, 0.94);
ny = clamp(0.5 + 0.5 * Math.sin((t / 1700) * Math.PI * 2 + 1), 0.08, 0.94);
}
const g = goal();
if (!placed)
snap();
px += (g[0] - px) * FOLLOW_LERP;
py += (g[1] - py) * FOLLOW_LERP;
}
function snap() {
const g = goal();
px = g[0];
py = g[1];
placed = true;
}
function render() {
const w = root.clientWidth;
const h = root.clientHeight;
const ax = nx * w;
const ay = ny * h;
anchor.style.transform = "translate3d(" + (ax - ANCHOR_R).toFixed(1) + "px," + (ay - ANCHOR_R).toFixed(1) + "px,0)";
pop.style.transform = "translate3d(" + px.toFixed(1) + "px," + py.toFixed(1) + "px,0)";
const cx = px + POP_W / 2;
const cy = py + POP_H / 2;
const len = Math.hypot(cx - ax, cy - ay);
tether.style.transform = "translate3d(" + ax.toFixed(1) + "px," + (ay - 1).toFixed(1) + "px,0) rotate(" + Math.atan2(cy - ay, cx - ax).toFixed(4) + "rad) scale(" + len.toFixed(1) + ",1)";
bound.style.transform = "translate3d(" + EDGE_PAD + "px," + EDGE_PAD + "px,0)";
bound.style.width = Math.max(0, w - 2 * EDGE_PAD) + "px";
bound.style.height = Math.max(0, h - 2 * EDGE_PAD) + "px";
say("over " + Math.round(Math.hypot(overX, overY)) + " stretch " + Math.round(Math.hypot(rubber(overX), rubber(overY))));
}
function moveTo(e) {
const p = local(e);
nx = clamp(p.x / root.clientWidth, 0.04, 0.96);
ny = clamp(p.y / root.clientHeight, 0.06, 0.96);
sync();
}
on(root, "pointerdown", (e) => {
stopDemo();
dragging = true;
root.setPointerCapture(e.pointerId);
moveTo(e);
});
on(root, "pointermove", (e) => {
if (dragging)
moveTo(e);
});
on(root, "pointerup", () => {
dragging = false;
});
on(root, "pointercancel", () => {
dragging = false;
});
on(anchor, "keydown", (e) => {
const dx = e.key === "ArrowRight" ? 1 : e.key === "ArrowLeft" ? -1 : 0;
const dy = e.key === "ArrowDown" ? 1 : e.key === "ArrowUp" ? -1 : 0;
if (dx === 0 && dy === 0)
return;
e.preventDefault();
stopDemo();
nx = clamp(nx + dx * 0.06, 0.04, 0.96);
ny = clamp(ny + dy * 0.08, 0.06, 0.96);
sync();
});
snap();
const FRAME_MS = 1000 / 60;
function frame(ts) {
if (t0 < 0) {
t0 = ts;
last = ts;
}
acc += Math.min(100, ts - last);
last = ts;
let n = 0;
while (acc >= FRAME_MS && n < 6) {
clock += FRAME_MS;
tick(clock);
acc -= FRAME_MS;
n += 1;
}
if (n === 6)
acc = 0;
render();
raf = requestAnimationFrame(frame);
}
const ro = new ResizeObserver(() => sync());
ro.observe(root);
if (reduce) {
stopDemo();
snap();
render();
}
else {
raf = requestAnimationFrame(frame);
}
return () => {
cancelAnimationFrame(raf);
offs.forEach((f) => f());
ro.disconnect();
};
})();// RubberEdgeDrag - vanilla 판(05-pm-rubber-edge-drag) 의 React 포트. 같은 계산을 useEffect 안에서 돌린다.
import { useEffect, useRef } from "react";
import styles from "./RubberEdgeDrag.module.scss";
export default function RubberEdgeDrag() {
const rootRef = useRef<HTMLDivElement | null>(null);
useEffect(() => {
const rootEl = rootRef.current;
if (!rootEl) return;
const root: HTMLElement = rootEl;
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let demo = !reduce;
let raf = 0;
let t0 = -1;
let last = -1;
let acc = 0;
let clock = 0;
const offs: Array<() => void> = [];
const live = root.querySelector("[data-r=live]") as HTMLElement;
function q<T extends Element>(sel: string): T {
return root.querySelector(sel) as T;
}
function clamp(v: number, lo: number, hi: number): number {
return Math.max(lo, Math.min(hi, v));
}
function seg(t: number, a: number, b: number): number {
const c = clamp((t - a) / (b - a), 0, 1);
return c * c * (3 - 2 * c);
}
function on(el: EventTarget, type: string, fn: (e: any) => void): void {
el.addEventListener(type, fn);
offs.push(() => el.removeEventListener(type, fn));
}
function stopDemo(): void {
if (!demo) return;
demo = false;
root.removeAttribute("data-demo");
root.classList.remove("is-demo");
live.setAttribute("aria-live", "polite");
}
if (reduce) live.setAttribute("aria-live", "polite");
let shown = "";
function say(s: string): void {
if (s === shown) return;
shown = s;
live.textContent = s;
}
function makeRng(seed: number): () => number {
let s = seed >>> 0;
return () => {
s = (Math.imul(s, 1664525) + 1013904223) >>> 0;
return s / 4294967296;
};
}
function local(e: PointerEvent): { x: number; y: number } {
const r = root.getBoundingClientRect();
return { x: e.clientX - r.left, y: e.clientY - r.top };
}
function sync(): void {
if (reduce) {
snap();
render();
}
}
// 기법: 팝오버 목표 위치가 허용 범위를 넘으면 넘은 거리에 저항(RUBBER)을 곱하고 LAG 에서 포화시킨 뒤, FOLLOW_LERP 관성으로 쫓아간다.
const FOLLOW_LERP = 0.14;
const RUBBER = 0.55;
const EDGE_PAD = 20;
const LAG = 28;
const POP_W = 116;
const POP_H = 56;
const ANCHOR_R = 14;
const LIFT = 10;
const SWEEP_MS = 2600;
const anchor = q<HTMLElement>("[data-r=anchor]");
const pop = q<HTMLElement>("[data-r=pop]");
const tether = q<HTMLElement>("[data-r=tether]");
const bound = q<HTMLElement>("[data-r=bound]");
let nx = reduce ? 0.94 : 0.5;
let ny = reduce ? 0.2 : 0.55;
let px = 0;
let py = 0;
let placed = false;
let dragging = false;
let overX = 0;
let overY = 0;
function rubber(over: number): number {
const o = Math.abs(over);
const r = LAG * (1 - 1 / (1 + (o * RUBBER) / LAG));
return over < 0 ? -r : r;
}
function resist(v: number, lo: number, hi: number): number {
if (v < lo) return lo + rubber(v - lo);
if (v > hi) return hi + rubber(v - hi);
return v;
}
function goal(): number[] {
const w = root.clientWidth;
const h = root.clientHeight;
const tx = nx * w - POP_W / 2;
const ty = ny * h - ANCHOR_R - LIFT - POP_H;
const lx = EDGE_PAD;
const hx = w - EDGE_PAD - POP_W;
const ly = EDGE_PAD;
const hy = h - EDGE_PAD - POP_H;
overX = tx < lx ? tx - lx : tx > hx ? tx - hx : 0;
overY = ty < ly ? ty - ly : ty > hy ? ty - hy : 0;
return [resist(tx, lx, hx), resist(ty, ly, hy)];
}
function tick(ms: number): void {
if (demo) {
const t = Math.max(0, ms - 200);
nx = clamp(0.5 + 0.58 * Math.sin((t / SWEEP_MS) * Math.PI * 2), 0.06, 0.94);
ny = clamp(0.5 + 0.5 * Math.sin((t / 1700) * Math.PI * 2 + 1), 0.08, 0.94);
}
const g = goal();
if (!placed) snap();
px += (g[0] - px) * FOLLOW_LERP;
py += (g[1] - py) * FOLLOW_LERP;
}
function snap(): void {
const g = goal();
px = g[0];
py = g[1];
placed = true;
}
function render(): void {
const w = root.clientWidth;
const h = root.clientHeight;
const ax = nx * w;
const ay = ny * h;
anchor.style.transform = "translate3d(" + (ax - ANCHOR_R).toFixed(1) + "px," + (ay - ANCHOR_R).toFixed(1) + "px,0)";
pop.style.transform = "translate3d(" + px.toFixed(1) + "px," + py.toFixed(1) + "px,0)";
const cx = px + POP_W / 2;
const cy = py + POP_H / 2;
const len = Math.hypot(cx - ax, cy - ay);
tether.style.transform = "translate3d(" + ax.toFixed(1) + "px," + (ay - 1).toFixed(1) + "px,0) rotate(" + Math.atan2(cy - ay, cx - ax).toFixed(4) + "rad) scale(" + len.toFixed(1) + ",1)";
bound.style.transform = "translate3d(" + EDGE_PAD + "px," + EDGE_PAD + "px,0)";
bound.style.width = Math.max(0, w - 2 * EDGE_PAD) + "px";
bound.style.height = Math.max(0, h - 2 * EDGE_PAD) + "px";
say("over " + Math.round(Math.hypot(overX, overY)) + " stretch " + Math.round(Math.hypot(rubber(overX), rubber(overY))));
}
function moveTo(e: PointerEvent): void {
const p = local(e);
nx = clamp(p.x / root.clientWidth, 0.04, 0.96);
ny = clamp(p.y / root.clientHeight, 0.06, 0.96);
sync();
}
on(root, "pointerdown", (e: PointerEvent) => {
stopDemo();
dragging = true;
root.setPointerCapture(e.pointerId);
moveTo(e);
});
on(root, "pointermove", (e: PointerEvent) => {
if (dragging) moveTo(e);
});
on(root, "pointerup", () => {
dragging = false;
});
on(root, "pointercancel", () => {
dragging = false;
});
on(anchor, "keydown", (e: KeyboardEvent) => {
const dx = e.key === "ArrowRight" ? 1 : e.key === "ArrowLeft" ? -1 : 0;
const dy = e.key === "ArrowDown" ? 1 : e.key === "ArrowUp" ? -1 : 0;
if (dx === 0 && dy === 0) return;
e.preventDefault();
stopDemo();
nx = clamp(nx + dx * 0.06, 0.04, 0.96);
ny = clamp(ny + dy * 0.08, 0.06, 0.96);
sync();
});
snap();
const FRAME_MS = 1000 / 60;
function frame(ts: number): void {
if (t0 < 0) {
t0 = ts;
last = ts;
}
acc += Math.min(100, ts - last);
last = ts;
let n = 0;
while (acc >= FRAME_MS && n < 6) {
clock += FRAME_MS;
tick(clock);
acc -= FRAME_MS;
n += 1;
}
if (n === 6) acc = 0;
render();
raf = requestAnimationFrame(frame);
}
const ro = new ResizeObserver(() => sync());
ro.observe(root);
if (reduce) {
stopDemo();
snap();
render();
} else {
raf = requestAnimationFrame(frame);
}
return () => {
cancelAnimationFrame(raf);
offs.forEach((f) => f());
ro.disconnect();
};
}, []);
return (
<main className={styles["stage"]}>
<div ref={rootRef} className={styles["pm"] + " is-demo"} data-demo="1">
<p className={styles["pm__live"]} data-r="live" aria-live="off" aria-atomic="true"></p>
<i className={styles["pm__bound"]} data-r="bound"></i>
<i className={styles["pm__tether"]} data-r="tether"></i>
<div className={styles["pm__pop"]} data-r="pop"><b className={styles["pm__ttl"]}>메모</b><span className={styles["pm__txt"]}>끌어서 옮기세요</span></div>
<button className={styles["pm__anchor"]} data-r="anchor" type="button" aria-label="핀 · 방향키로 옮기기"></button>
</div>
</main>
);
}05 A Popover That Follows by Inertia and Resists Like Rubber at the Edge — Measured area 54.481% · moving frames 23/23Original by GODRICH · SCSS and React ship in the zip · CSS Popover: 9 Ways to Anchor, Flip and Spring
The post this demo lives in
CSS Popover: 9 Ways to Anchor, Flip and SpringA css popover can clip at screen edges, point its arrow the wrong way and overlap its neighbors — scenes 01, 04 and 09 f See all demos in this post
If you like this effect - similar demos
Interactions01 A Wireframe Polyhedron That Eases Toward the Pointer and Fades With DepthJavaScript 3D Animation: 9 Pointer-Led Models
Interactions05 Pointer parallax — the math behind a diorama that gently followsIsometric CSS: 9 True-Projection 3D Dioramas
Interactions05 Like/Nope Stamp That Fades In With Drag DistanceCard Swipe UI: 9 Physics-Based Deck Patterns