<div id="ball"></div>
body {
display: flex;
align-items: center;
justify-content: flex-start;
min-height: 100vh;
padding: 0;
margin: 0;
margin-left: 10px;
}
#ball {
width: 100px;
height: 100px;
border-radius: 50%;
background: #32005c;
}
View Compiled
function tween({
from = 0,
to = 1,
duration = 300,
ease = easeOut,
onUpdate
} = {}) {
const delta = to - from;
const startTime = performance.now();
function update(currentTime) {
const elapsed = currentTime - startTime;
const progress = Math.min(elapsed / duration, 1);
const latest = from + ease(progress) * delta;
if (onUpdate) onUpdate(latest);
if (progress < 1) {
requestAnimationFrame(update);
}
}
requestAnimationFrame(update);
}
function easeOut(progress, power = 2) {
return 1 - (1 - progress) ** power;
}
const ball = document.getElementById("ball");
tween({
from: 0,
to: 300,
duration: 500,
onUpdate: v => {
ball.style.transform = `translateX(${v}px) translateZ(0)`;
}
});
View Compiled
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.