DEV Community

Cover image for How to rotate image clockwise anticlockwise onclick button javascript | Akashminds
Akash Chauhan
Akash Chauhan

Posted on • Updated on

How to rotate image clockwise anticlockwise onclick button javascript | Akashminds

Assuming you would like 8 sentences in 2-3 paragraphs:

With a little bit of coding, you can use JavaScript to rotate images. This is a great way to add some interactivity to your web page without having to use a heavy plugin. You can also use CSS to rotate images, but JavaScript gives you more control over the animation.

With a little bit of code, you canrotate imagesonclick buttons with JavaScript. It's a fairly simple process that only requires a few lines of code.

Knowing how to rotate images clockwise and anticlockwise is essential for anyone who wants to create beautiful or practical designs.

In order to rotate an image using CSS, the rotate property must be used. This property is a part of the transform property. It takes one value, which is an angle in degrees.

In this article, I am going to show you how you can rotate image clockwise anticlockwise onclick button javascript

Learning Objectives:

  • 1. Rotate image css properties
  • 2. Get the image element using JavaScript
  • 3. JavaScript click event to change css
  • 4. Rotate image clockwise onclick JavaScript
  • 5. Rotate image anticlockwise onclick JavaScript
  • 6. Summary

Rotate image css properties

There are many ways to rotate an image on a web page using CSS. One way is to use the CSS transform property. This property allows you to rotate, scale, skew, or translate an element. To rotate an image, you can use thetransform property with the "rotate" value.

The number of degrees that you want to rotate the image can be set as a positive or negative number. You can also set the rotation to happen around a certain point, such as the center of the image, by using the transform-origin property.

Another way to rotate an image on a web page is by using CSS animations. To do this, you first need to create a @keyframes rule with a name that you will use later.

In this rule, you will specify the amount of rotation that should happen at specific points in time.

The image can be rotated clockwise or anticlockwise.

For example, to rotate an image clockwise by 90 degrees clockwise, the code would be as follows:

img {

  transform: rotate(90deg);

}
Enter fullscreen mode Exit fullscreen mode

If you want to rotate an image anticlockwise by 90 degrees, you would use:

img {

  transform: rotate(-90deg);

}
Enter fullscreen mode Exit fullscreen mode

Get the image element using JavaScript

Image description
In order to get the image element using JavaScript, one must first understand how the img tag works in HTML. The img tag is used to embed an image into a web page.

The image tag has two attributes, src and alt. The src attribute specifies the URL of the image to be displayed. The alt attribute provides alternate text for the image, if the image cannot be displayed.

To get the image element using JavaScript, one can use the getElementById method. This method returns the element with the specified id attribute.

For example, if one wanted to get the image element with an id of "myImage", they would use the following code:

<html>
   <body>
    <h1>How to rotate image clockwise anticlockwise onclick button javascript?</h1>
    <img alt="rotate image using javascript" border="0" data-original-height="495" data-original-width="742" height="133" id="myImage" src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fwkpk6tj0se2rhodoerq.jpg" width="200" />
    <script>
          var myImage = document.getElementById("myImage");
          console.log(myImage)
    </script>
   </body>
</html>
Enter fullscreen mode Exit fullscreen mode

Once you have obtained a reference to the image element, you can access its properties and methods.

Result:

Image description

JavaScript click event to change css

To attach a function to button element element, you first need to create the function. The function can be anything you want it to be, but it must be defined before it can be attached to an element.

To attach the function to an element, you use the onclick attribute.

For example:

onclick="onButtonClick()"

This will cause the onButtonClick() function to be executed when the element is clicked. You can also define inline JavaScript functions using the onclick attribute.

Rotate image clockwise onclick JavaScript

When you click on an image, sometimes you want it to rotate. Rotating an image on a webpage can be done using JavaScript. Here's how to do it.

First, you need to have an image element on your HTML page like we done above. Then, you'll need to set up a function to rotate the image when it's clicked like in the previous step.

In this function, you'll use the CSS transform property to rotate the image by a certain number of degrees,

For that we'll first need to access the transform property so that you could give it some value according to the angle you need according to your requirements so first I am going to rotate it clockwise.

<img id="myImage" alt="rotate image using javascript" border="0"
data-original-height="495" data-original-width="742" height="133" src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fwkpk6tj0se2rhodoerq.jpg" width="200" /> <br/><button onclick="rotateImageClockWise()">Rotate image clockwise</button>

<script>
  function rotateImageClockWise() {
    var myImage = document.getElementById("myImage");
    myImage.style.transform = 'rotate(90deg)';
  }
</script>
Enter fullscreen mode Exit fullscreen mode

Rotate image clockwise example:

Image description

Rotate image anticlockwise onclick JavaScript

In this case there is nothing specific required , You will just have to change the transform value in minus (-) so that it could be rotated anticlockwise just like this example below.

<img id="myImage" alt="rotate image using javascript" border="0"
data-original-height="495" data-original-width="742" height="133" src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fwkpk6tj0se2rhodoerq.jpg" width="200" /> <br/><button onclick="rotateImageAntiClockWise()">Rotate image Anticlockwise</button>

<script>
  function rotateImageAntiClockWise() {
    var myImage = document.getElementById("myImage");
    myImage.style.transform = 'rotate(-90deg)';
  }
</script>
Enter fullscreen mode Exit fullscreen mode

Rotate image Anticlockwise example:

Image description

Summary

This article has shown you how to rotate an image clockwise or anticlockwise onclick using button javascript.

First, the function was defined.
Second, the event listener was added to the button.
Finally, the function was called when the button was clicked. This can be useful for creating games or interactive applications.

By following the steps outlined in this article, you can easily add this functionality to your own web applications.

Give it a try today!
Original source at: https://akashminds.com/

Related Articles

The 10 essential frontend developer skills that you will need to know in 2023.
Top 9 commonly used javascript functions with example
How much Javascript is required for Selenium Automation tester ?

Top comments (0)