<input type="checkbox" id="slowmo" />
<label for="slowmo">Slowmo!</label>
<div>🐶</div>
body { font-size: 32px; }
input { height: 32px; width: 32px; }
div { font-size: 64px; width: fit-content; }
div {
position: absolute;
animation-name: spin;
animation-delay: 0s;
animation-duration: 2s;
animation-timing-function: linear;
animation-iteration-count: infinite;
transform-origin: 100% 100%;
}
@keyframes spin {
from {
z-index: 0;
}
to {
transform: rotate(360deg);
z-index: 100;
}
}
const div = document.querySelector('div');
const slowmo = document.querySelector('input');
const fastSpinDurationSecond = 2;
const slowSpinDurationSecond = 5;
const spinAnimationName = 'spin';
slowmo.addEventListener('change', (e) => {
const progress = parseInt(window.getComputedStyle(div).getPropertyValue('z-index')) / 100;
let duration = 0;
if (slowmo.checked) {
duration = slowSpinDurationSecond;
} else {
duration = fastSpinDurationSecond;
}
div.style.setProperty('animation-duration', duration.toString() + 's');
div.style.setProperty('animation-delay', (-1 * duration * progress).toString() + 's');
// resetting playback time, by removing the animation and adding it back.
div.style.setProperty('animation-name', 'none');
setTimeout(()=> {
div.style.setProperty('animation-name', spinAnimationName);
}, 0);
})
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.