Home > Article > Web Front-end > How to make pictures rotate in jquery
Image rotation is a common effect in web design, which can make web pages more vivid and interesting. In front-end development, we often use jQuery to achieve this effect. This article will introduce how to use jQuery to realize image rotation.
1. Make a picture
First you need to prepare a picture to be rotated. It can be a simple circle, or a complex picture with text and patterns. This article will take a simple circle diagram as an example.
2. Write HTML and CSS code
Put the image into the HTML file, as shown below:
<div class="rotate"> <img src="circle.png"> </div>
In order to make the image rotate, we need to modify its CSS style. The specific modification method is as follows:
.rotate { display: inline-block; position: relative; margin: 50px; } .rotate img { position: absolute; top: 0; left: 0; width: 200px; height: 200px; }
The main purpose of the above modification is to set the picture and its containing box to relative and absolute positioning, and at the same time set the width, height and position of the picture.
3. Use jQuery to achieve image rotation
Next, we will use jQuery to achieve the image rotation effect. There is a very convenient function animate() in jQuery, which can be used to achieve a variety of animation effects, including rotation. Here, we will use this function to implement image rotation.
The specific code is as follows:
$(document).ready(function() { $(".rotate img").animate({ rotate: '360deg' },{ duration: 2000, step: function(now, fx) { $(this).css('-webkit-transform','rotate('+now+'deg)'); $(this).css('-moz-transform','rotate('+now+'deg)'); $(this).css('transform','rotate('+now+'deg)'); } }); });
The above code does the following things:
Run the above code to make the picture rotate!
4. Complete code example
The following is the complete HTML and jQuery code:
HTML code:
<html> <head> <meta charset="UTF-8"> <title>jQuery图片自转示例</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="rotate"> <img src="circle.png"> </div> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script src="script.js"></script> </body> </html>
CSS code:
.rotate { display: inline-block; position: relative; margin: 50px; } .rotate img { position: absolute; top: 0; left: 0; width: 200px; height: 200px; }
jQuery code:
$(document).ready(function() { $(".rotate img").animate({ rotate: '360deg' },{ duration: 2000, step: function(now, fx) { $(this).css('-webkit-transform','rotate('+now+'deg)'); $(this).css('-moz-transform','rotate('+now+'deg)'); $(this).css('transform','rotate('+now+'deg)'); } }); });
Summary
Through the above steps, you can use jQuery to achieve the image rotation effect on the web page. In addition, jQuery can also be used to implement various other animation effects, such as fade in and fade out, sliding, etc., which can be used to enhance the interactivity and beauty of web pages. Hope this article helps you!
The above is the detailed content of How to make pictures rotate in jquery. For more information, please follow other related articles on the PHP Chinese website!