JavaScript
How to move image or element with JavaScript
January 04, 2023
1 min
Here is a simple example of how you can use JavaScript to rotate an image.
First, you will need to have an image element in your HTML with an id attribute that you can use to access it in your JavaScript code. For example:
<img id="my-image" src="path/to/image.jpg" alt="My Image">
Next, you can use the following JavaScript code to rotate the image:
// Get the image element var image = document.getElementById('my-image'); // Set the rotation angle var angle = 45; // Rotate the image image.style.transform = 'rotate(' + angle + 'deg)';
This code will rotate the image by 45 degrees clockwise. You can adjust the value of the angle
variable to change the amount of rotation.
You can also use other CSS transform functions, such as scale
and translate
, to manipulate the image in various ways.