<main>
<h2>CSS Animations: the <code>animation-duration</code> Property</h2>
<p>Use the inputs below to set a duration for the animation. You can choose to define the duration using seconds or milliseconds.</p>
<div class="box">
</div>
<label for="duration">Set a duration:</label> <input id="duration" type="number" value="1" min="0" step="0.1">
<select id="unit">
<option value="s">s</option>
<option value="ms">ms</option>
</select>
<br>
<button>Animate the Box</button> <button disabled>Reset</button>
</main>
body {
font-family: Arial, sans-serif;
font-size: 20px;
padding: 0 20px;
}
main {
text-align: center;
margin: 0 auto;
max-width: 800px;
}
p {
text-align: left;
padding: 0 20px;
}
code {
color: firebrick;
}
.box {
width: 100px;
height: 100px;
background: lightgreen;
border: solid 2px;
margin-bottom: 20px;
}
.box-animate {
animation-name: moveObject;
animation-timing-function: linear;
}
@keyframes moveObject {
from {
transform: translateX(0);
}
50% {
transform: translateX(300px);
}
to {
transform: translateX(300px) scale(1.5);
}
}
input[type=number] {
width: 100px;
}
button {
margin-top: 20px;
}
let myObject = document.querySelector('.box'),
btn1 = document.querySelector('button'),
btn2 = document.querySelectorAll('button')[1];
btn1.addEventListener('click', function () {
myObject.style.animationDuration = document.querySelector('#duration').value + document.querySelector('#unit').value;
myObject.classList.add('box-animate');
this.disabled = true;
btn2.disabled = false;
}, false);
btn2.addEventListener('click', function () {
myObject.classList.remove('box-animate');
this.disabled = true;
btn1.disabled = false;
}, false);
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.