Many people don't know how to rotate images in JavaScript. It's actually quite simple. With a few lines of code, you can rotate an image 90 degrees.
To rotate an image, you need to use the rotate() method. This method takes an angle as a parameter. The angle is measured in degrees. So, to rotate an image 90 degrees, you would pass in the value 90.
You can also use negative values to rotate an image in the opposite direction. For example, if you wanted to rotate an image 180 degrees, you would pass in the value -180.
we'll go over how to rotate an image 90 degrees using a simple JavaScript onclick function.
This can be useful for displaying different images depending on the orientation of the device, or for providing a more intuitive interface for users to flip between images.
For instance, if you have a product image that can be rotated to show different sides, you could use this technique to give the user some control over which view they see.
The first thing we need to do is add an onclick event handler to our image. This will trigger the rotation code when the image is clicked.
We'll also need to define a function to perform the actual rotation. The CSS transform property can be used for rotating elements, and we can use it here with a rotate value of 90 degrees.
Learning Objectives:
- Css transform:rotate()
- Get the image element using JavaScript
- Add JavaScript click event to the image
- Rotate image clockwise onclick JavaScript
- Rotate image anticlockwise onclick JavaScript
- Summary
Css transform:rotate()
CSS transform is a property that allows you to change the position, size, and shape of an element. You can also use it to rotate, scale, and skew an element.
The rotate function lets you rotate an element around a certain point. if you wanted to rotate an image of a clockwise by 90 degrees, you would use the following code: img { transform: rotate(90deg); } The scale function lets you change the size of an element.
For example, if you wanted to make an image twice as large, you would use the following code: img { transform: scale(2); } The skew function lets you distort an element.
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);
}
If you want to rotate an image anticlockwise by 90 degrees, you would use:
img {
transform: rotate(-90deg);
}
Get the image element using JavaScript
Test Image |
<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://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgcv1YL2VCTEUFLYhe_tejEF_eOy8MstSNbOxqu9_0caP2npp3qNvJNEatSJts5EiGrJSL92QeOLGjSfuckP2OIjuwIsaoMTXx4cswWiXR-15ncmb_4tO9NGYfyrQMtGCJgPMCH-uVLybQIzHGk8MxRekMEb2xn1xXY5oQS5sdeapk4ho14AR7CpjjW/w200-h133/rotate%20image%20javascript.jpg" width="200" />
<script>
var myImage = document.getElementById("myImage");
console.log(myImage)
</script>
</body>
</html>
Rotate image clockwise onclick JavaScript
<img id="myImage" alt="rotate image using javascript" border="0"
data-original-height="495" data-original-width="742" height="133" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgcv1YL2VCTEUFLYhe_tejEF_eOy8MstSNbOxqu9_0caP2npp3qNvJNEatSJts5EiGrJSL92QeOLGjSfuckP2OIjuwIsaoMTXx4cswWiXR-15ncmb_4tO9NGYfyrQMtGCJgPMCH-uVLybQIzHGk8MxRekMEb2xn1xXY5oQS5sdeapk4ho14AR7CpjjW/w200-h133/rotate%20image%20javascript.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>
<img id="myImage" alt="rotate image using javascript" border="0"
data-original-height="495" data-original-width="742" height="133" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgcv1YL2VCTEUFLYhe_tejEF_eOy8MstSNbOxqu9_0caP2npp3qNvJNEatSJts5EiGrJSL92QeOLGjSfuckP2OIjuwIsaoMTXx4cswWiXR-15ncmb_4tO9NGYfyrQMtGCJgPMCH-uVLybQIzHGk8MxRekMEb2xn1xXY5oQS5sdeapk4ho14AR7CpjjW/w200-h133/rotate%20image%20javascript.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>
2 Comments
well explained
ReplyDeleteIt helped me
ReplyDelete