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="/static/imghwm/default1.png" data-src="circle.png" class="lazy" alt="How to make pictures rotate in jquery" > </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:
- After the page is loaded, look for the class ".rotate img" element and rotate it 360 degrees using the animate() function.
- In the animate() function, the animation transition duration is set to 2000 milliseconds, which is 2 seconds.
- 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="/static/imghwm/default1.png" data-src="circle.png" class="lazy" alt="How to make pictures rotate in jquery" > </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!

Classesarebetterforaccessibilityinwebdevelopment.1)Classescanbeappliedtomultipleelements,ensuringconsistentstylesandbehaviors,whichaidsuserswithdisabilities.2)TheyfacilitatetheuseofARIAattributesacrossgroupsofelements,enhancinguserexperience.3)Classe

Classselectorsarereusableformultipleelements,whileIDselectorsareuniqueandusedonceperpage.1)Classes,denotedbyaperiod(.),areidealforstylingmultipleelementslikebuttons.2)IDs,denotedbyahash(#),areperfectforuniqueelementslikeanavigationmenu.3)IDshavehighe

In CSS style, the class selector or ID selector should be selected according to the project requirements: 1) The class selector is suitable for reuse and is suitable for the same style of multiple elements; 2) The ID selector is suitable for unique elements and has higher priority, but should be used with caution to avoid maintenance difficulties.

HTML5hasseverallimitationsincludinglackofsupportforadvancedgraphics,basicformvalidation,cross-browsercompatibilityissues,performanceimpacts,andsecurityconcerns.1)Forcomplexgraphics,HTML5'scanvasisinsufficient,requiringlibrarieslikeWebGLorThree.js.2)I

Yes,onestylecanhavemoreprioritythananotherinCSSduetospecificityandthecascade.1)Specificityactsasascoringsystemwheremorespecificselectorshavehigherpriority.2)Thecascadedeterminesstyleapplicationorder,withlaterrulesoverridingearlieronesofequalspecifici

ThesignificantgoalsofHTML5aretoenhancemultimediasupport,ensurehumanreadability,maintainconsistencyacrossdevices,andensurebackwardcompatibility.1)HTML5improvesmultimediawithnativeelementslikeand.2)ItusessemanticelementsforbetterreadabilityandSEO.3)Its

React'slimitationsinclude:1)asteeplearningcurveduetoitsvastecosystem,2)SEOchallengeswithclient-siderendering,3)potentialperformanceissuesinlargeapplications,4)complexstatemanagementasappsgrow,and5)theneedtokeepupwithitsrapidevolution.Thesefactorsshou

Reactischallengingforbeginnersduetoitssteeplearningcurveandparadigmshifttocomponent-basedarchitecture.1)Startwithofficialdocumentationforasolidfoundation.2)UnderstandJSXandhowtoembedJavaScriptwithinit.3)Learntousefunctionalcomponentswithhooksforstate


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!
