How to make pictures rotate in jquery

WBOY
Release: 2023-05-18 11:24:37
Original
538 people have browsed it

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>
Copy after login

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;
}
Copy after login
Copy after login

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)');
    }
  });
}); 
Copy after login
Copy after login

The above code does the following things:

  1. After the page is loaded, look for the class ".rotate img" element and rotate it 360 degrees using the animate() function.
  2. In the animate() function, the animation transition duration is set to 2000 milliseconds, which is 2 seconds.
  3. In the step callback function, the element is rotated according to the current rotation angle, and some browser manufacturer prefixes are added to ensure the compatibility of the animation effect in various browsers.

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>
Copy after login

CSS code:

.rotate {
  display: inline-block;
  position: relative;
  margin: 50px;
}
.rotate img {
  position: absolute;
  top: 0;
  left: 0;
  width: 200px;
  height: 200px;
}
Copy after login
Copy after login

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)');
    }
  });
}); 
Copy after login
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!