Directional Metric Delta
数值变动方向反馈
A signed comparison and brief directional change show how a metric moved from its previous value.
Where to use it
Add context to live counts, inventory, or monitoring measurements. State the comparison baseline alongside the signed change so movement has an interpretable reference.
Practical example
A ticket dashboard receives another support request. Open tickets goes from 38 to 39, and a brief upward cue with “+1 since last update” makes the change easy to read.
AI prompt
Add a signed delta to [metric]. Compute change from the immediate previous value, encode direction with symbol and color, cancel prior motion on rapid updates, and expose the updated value to assistive technology.
Example code
Save the standalone HTML as a file and open it in your browser. It uses the same code as the preview.
View HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Directional Metric Delta</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;
}
.ui { width: min(100%, 238px); display: grid; gap: 8px; position: relative; font-size: 12px; line-height: 1.35; }
.ui .surface { min-width: 0; border: 1px solid #676591; border-radius: 10px; background: #282744; padding: 8px; }
.ui .bar { display: flex; align-items: center; justify-content: space-between; gap: 6px; }
.ui .actions { display: flex; align-items: center; justify-content: center; flex-wrap: wrap; gap: 5px; }
.ui .muted { color: #cecae9; font-size: 11px; }
.ui .center { text-align: center; }
.ui .mono { font: 11px ui-monospace, monospace; font-variant-numeric: tabular-nums; }
.ui p { margin: 0; }
.ui .button { min-height: 30px; padding: 4px 7px; border-radius: 7px; border: 1px solid #79779f; background: #3d3a60; color: #f9f5ff; font-size: 11px; font-weight: 650; cursor: pointer; }
.ui .button:hover { background: #4e4976; }
.ui .primary { background: #c5eeec; border-color: #dafaee; color: #17213b; }
.ui .primary:hover { background: #e3fff9; }
.ui .button:disabled { opacity: .6; cursor: default; }
.ui .field { min-width: 0; width: 100%; border: 1px solid #79779f; border-radius: 6px; background: #1b1a31; color: #faf5ff; padding: 5px 7px; font-size: 12px; }
.ui .field::placeholder { color: #c2bcdd; }
.ui input[type="range"] { accent-color: #b8ebe0; width: 100%; min-width: 0; margin: 0; }
.ui button { font: inherit; }
.ui [hidden] { display: none !important; }
.ui progress { accent-color: #b8ebe0; }
@media (prefers-reduced-motion: reduce) {
.ui *, .ui *::after, .ui *::before { transition-duration: 0s !important; animation-duration: 0s !important; scroll-behavior: auto !important; }
}
.metric-value{font-size:36px;letter-spacing:-.07em;font-variant-numeric:tabular-nums}.delta{border-radius:20px;padding:4px 8px;background:#4d4770;color:#fff}.delta[data-direction=up]{background:#25574b;color:#b9f3d5}.delta[data-direction=down]{background:#683f55;color:#ffd5e6}.announcement{position:absolute;width:1px;height:1px;padding:0;overflow:hidden;clip-path:inset(50%)}
</style>
</head>
<body>
<main class="demo">
<div class="ui"><div class="surface metric"><div class="muted">Studio visitors</div><div class="bar"><strong class="metric-value">128</strong><span class="delta mono">—</span></div></div><div class="actions"><button class="button" type="button" data-change="-4">−4</button><button class="button primary" type="button" data-change="7">+7</button></div><span class="announcement" role="status"></span></div>
</main>
<script>
const number = document.querySelector('.metric-value');
const badge = document.querySelector('.delta');
const status = document.querySelector('[role=status]');
const reduce = matchMedia('(prefers-reduced-motion: reduce)');
let value = 128, animation = null;
for (const button of document.querySelectorAll('[data-change]')) button.addEventListener('click', () => {
const step = Number(button.dataset.change); value += step; number.textContent = value;
badge.dataset.direction = step > 0 ? 'up' : 'down'; badge.textContent = step > 0 ? '↑ +' + step : '↓ −' + Math.abs(step);
status.textContent = value + ' ' + badge.textContent;
animation?.cancel();
if (!reduce.matches) animation = number.animate([{ transform: 'translateY(' + (step > 0 ? 9 : -9) + 'px)', opacity: .45 }, { transform: 'translateY(0)', opacity: 1 }], { duration: 210, easing: 'ease-out' });
});
</script>
</body>
</html>Original example by UI Skills