How do I change the colors and speed of a library motion?
Any motion in the library can get a new color and speed without rewriting it. The idea is to gather colors and duration into CSS variables, so every later edit lives in one place.
Steps
- Copy the motion's code into one file and open it in the browser. Run it before any edit so you know how the original looks.
- Find the hex or rgb colors in the CSS and move them into variables on
:root(for example--accent), then replace each value withvar(--accent). Find and Replace in any editor does it. - Gather speed into a
--speedvariable: write the duration ascalc(0.6s / var(--speed)), so a higher value means a shorter time. If the motion is driven by JavaScript, multiply or divide its fixed numbers by the same factor. - For quick experiments, change the variable from JS with
root.style.setProperty('--accent', value), as the code below does. - After every change, check the new color's contrast (the ratios are in the contrast article) and test
prefers-reduced-motionto make sure the calm version still works.
Sources
- MDN, Using CSS custom properties: https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties
- NN/g, Executing UX Animations: duration and motion characteristics: https://www.nngroup.com/articles/animation-duration/
- MDN, prefers-reduced-motion: https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-reduced-motion
- W3C, Understanding 1.4.3 Contrast (Minimum): https://www.w3.org/WAI/WCAG22/Understanding/contrast-minimum.html
The code
HTML
<div class="card" dir="ltr" lang="en"><b>Company name</b></div>
<p>
<label>Color <input id="c" type="color" value="#ffd166"></label>
<label>Speed <input id="s" type="range" min="0.5" max="2" step="0.1" value="1"></label>
</p>
<style>
:root{--accent:#ffd166;--speed:1}
.card{display:inline-block;padding:1rem 1.5rem;background:#0d0e11;color:#fff;
border-inline-start:6px solid var(--accent);font:700 1.4rem Cairo,system-ui,sans-serif}
/* higher speed = shorter duration. 4 runs, so it always stops. */
@media (prefers-reduced-motion: no-preference){
.card{animation:pop calc(0.6s / var(--speed)) ease-out 4 alternate}
}
@keyframes pop{from{transform:scale(1)}to{transform:scale(1.06)}}
</style>
<script>
const root = document.documentElement;
document.getElementById('c').addEventListener('input',
e => root.style.setProperty('--accent', e.target.value));
document.getElementById('s').addEventListener('input',
e => root.style.setProperty('--speed', e.target.value));
</script>On speed: NN/g says most UI animations should run roughly 100 to 500 ms depending on complexity, and that 500 ms starts to feel like a drag. For video and ads these limits are not binding, but they are a good reference for interfaces.
The original code may have its colors written inline instead of as variables, which is why step 2 exists.
can't find the motion you need? Request it here