• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / jQuery rotate – How to Rotate an Image using jQuery

jQuery rotate – How to Rotate an Image using jQuery

February 14, 2022 Leave a Comment

We can use jQuery to rotate an image easily by using the css() method. We can target the css transform property using the css() method and use that to rotate our image or div.

$("image").css("transform","rotate(20deg)");

In the code above, you can change “20deg” to any degree amount you want to rotate the image by.


Let’s say we have the following HTML:

<div id="div1">
  <img src="http://theprogrammingexpert.com/wp-content/uploads/example-img1.png">
</div>

If we want to rotate the image by 90 degrees we can use the jQuery css() method in the following JavaScript code.

$("#div1 img").css("transform","rotate(90deg)");

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery("#div1 img").css("transform","rotate(90deg)");

Using jQuery to Rotate an Image with a Click

To rotate an image using jQuery, we can combine the css() method with a click event.

In this example, we will have an image that we will want to rotate by 30 degrees. We will have a button that will allow the user to rotate the image by 30 degrees each time.

Here is our simple HTML code:

<div id="div1">
  <img src="http://theprogrammingexpert.com/wp-content/uploads/example-img1.png">
  <div id="click-me">Rotate image</div>
</div>

We can utilize both the jQuery css() method and the jQuery click() method to rotate the image.

We can then use the css() method to target the css transform property and use it to rotate our image.

Below is the Javascript code which will allow the user to be able to rotate our image by 30degrees each time:

var rotateDegrees = 0;
$("#click-me").click(function(){
  rotateDegrees += 30;
  $("#div1 img").css("transform","rotate(" + rotateDegrees + "deg)");
});

The final code and output for this example of how to use jQuery to rotate an image is below:

Code Output:

Rotate image

Full Code:

<div id="div1">
<img src="http://theprogrammingexpert.com/wp-content/uploads/example-img1.png">
<div id="click-me">Rotate image</div>
</div>

<script>

var rotateDegrees = 0;
$("#click-me").click(function(){
  rotateDegrees += 30;
  $("#div1 img").css("transform","rotate(" + rotateDegrees + "deg)");
});

</script>

Hopefully this article has been useful for you to understand how to rotate an image using jQuery.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Set Select to the First Option
  • 2.  Using jQuery to Set Visibility
  • 3.  How to Set All Checkbox to Checked in jQuery
  • 4.  jQuery val Method – Get the Value from an Input Field
  • 5.  jQuery mouseover – An Interactive Example of the mouseover Method
  • 6.  Using jQuery to Get the Value of a Radio Button
  • 7.  Using jQuery to Add Class to HTML Element
  • 8.  Using jQuery to Add Required Attribute to Input Field
  • 9.  Using jQuery to Check if Element Has Class
  • 10.  Using jQuery to Empty the Contents of an HTML Element

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy