To make your image rotate just once, simply remove the infinite iteration count from your CSS animation:
.spinner {
-webkit-animation: spin 1s linear;
-moz-animation: spin 1s linear;
animation: spin 1s linear;
-webkit-animation-fill-mode: forwards;
animation-fill-mode: forwards;
animation-delay: 400ms; /* start after 400ms */
}
This adjustment allows the spinning image to naturally stop at 360° without any abrupt changes. Plus, you can set an animation-delay
of 400ms
if you prefer starting the rotation with a delay instead of using JavaScript and setTimeout
:
.image {
position: absolute;
top: 50%;
left: 50%;
width: 120px;
height: 120px;
margin: -60px 0 0 -60px;
}
.spinner {
-webkit-animation: spin 1s linear;
-moz-animation: spin 1s linear;
animation: spin 1s linear;
-webkit-animation-fill-mode: forwards;
animation-fill-mode: forwards;
animation-delay: 400ms;
}
@-moz-keyframes spin {
100% {
-moz-transform: rotate(360deg);
}
}
@-webkit-keyframes spin {
100% {
-webkit-transform: rotate(360deg);
}
}
@keyframes spin {
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img class="image spinner" src="http://makeameme.org/media/templates/120/grumpy_cat.jpg" alt="" width="120" height="120">
To specify how many times the animation should repeat, include the count in the animate
CSS style. For example, to have it spin three times:
animation: spin 1s linear 3;
.image {
position: absolute;
top: 50%;
left: 50%;
width: 120px;
height: 120px;
margin: -60px 0 0 -60px;
}
.spinner {
-webkit-animation: spin 1s linear 3;
-moz-animation: spin 1s linear 3;
animation: spin 1s linear 3;
-webkit-animation-fill-mode: forwards;
animation-fill-mode: forwards;
animation-delay: 400ms;
}
@-moz-keyframes spin {
100% {
-moz-transform: rotate(360deg);
}
}
@-webkit-keyframes spin {
100% {
-webkit-transform: rotate(360deg);
}
}
@keyframes spin {
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img class="image spinner" src="http://makeameme.org/media/templates/120/grumpy_cat.jpg" alt="" width="120" height="120">
If you desire a delay between each spin, define keyframes that pause the rotation and adjust the animation duration accordingly:
.image {
position: absolute;
top: 50%;
left: 50%;
width: 120px;
height: 120px;
margin: -60px 0 0 -60px;
}
.spinner {
-webkit-animation: spin 2s linear 3;
-moz-animation: spin 2s linear 3;
animation: spin 2s linear 3;
-webkit-animation-fill-mode: forwards;
animation-fill-mode: forwards;
animation-delay: 400ms;
}
@-moz-keyframes spin {
50%, 100% {
-moz-transform: rotate(360deg);
}
}
@-webkit-keyframes spin {
50%, 100% {
-webkit-transform: rotate(360deg);
}
}
@keyframes spin {
50%, 100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img class="image spinner" src="http://makeameme.org/media/templates/120/grumpy_cat.jpg" alt="" width="120" height="120">