How to make your Image Spin Continuously in JavaScript

How to make your Image Spin Continuously in JavaScript

Introduction

In this tutorial we will create a How to make your Image Spin Continuously in JavaScript. This tutorial purpose is to allow you to rotate your displayed image. This will cover all the basic function that will search a list. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system if you want to add some animation to your image. I will give my best to provide you the easiest way of creating this program Image Spin. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface to our application. This code will only display image and button. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.                 <style>
  7.                         #image {
  8.                                 animation: rotation 1s linear 0s infinite;
  9.                                 animation-play-state: paused;
  10.                         }
  11.  
  12.                         @keyframes rotation {
  13.                                 to {
  14.                                         transform: rotate(360deg)
  15.                                 }
  16.                         }
  17.                 </style>
  18.         </head>
  19.         <nav class="navbar navbar-default">
  20.                 <div class="container-fluid">
  21.                         <a href="https://sourcecodester.com" class="navbar-brand">Sourcecodester</a>
  22.                 </div>
  23.         </nav>
  24.         <div class="col-md-3"></div>
  25.         <div class="col-md-6 well">
  26.                 <h3 class="text-primary">How to make your Image Spin Continuously</h3>
  27.                 <hr style="border-top:1px dotted #ccc;"/>
  28.                 <button class="btn btn-default" onclick="rotateImage(this);">Spin</button>
  29.                 <br />
  30.                 <img style="margin:20px;" src="wheel.png" id="image" height="400px" width="400px"/>
  31.         </div>
  32. <script src="script.js"></script>
  33. </body>
  34. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will dynamically spin your image continuously when clicked. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function rotateImage(val) {
  2.   let image = document.getElementById("image");
  3.         if (image.style.webkitAnimationPlayState == "running") {
  4.                 image.style.webkitAnimationPlayState = "paused";
  5.                 val.innerHTML="Spin";
  6.         } else {
  7.                 image.style.webkitAnimationPlayState = "running";
  8.                 val.innerHTML="Stop";
  9.         }
  10. }

The code is very simple and straightforward, in order for us to spin the image we will first a method called rotateImage(). This function will be trigger the event after the button is clicked. Inside this code we just simply change the animation control for the image by changing the image state

Output:

The How to make your Image Spin Continuously in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to make your Image Spin Continuously in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment