search
HomeWeb Front-endFront-end Q&Ajquery image rotate and enlarge

With the advancement of Internet technology, web design is becoming more and more creative. In design, it is often necessary to use image rotation and magnification effects to enhance the visual effect of the page. This is also an effect that most designers like very much. Therefore, in this article, we will introduce how to rotate and enlarge images using jQuery.

1. Realizing the rotation effect of images

Before realizing the rotation effect, we first need to have a certain understanding of the jQuery.rotate.js plug-in. This plug-in is a JavaScript library developed based on jQuery, which can achieve image rotation effects.

The following is the code to achieve the rotation effect:

html
<!DOCTYPE html>
<html>
<head>
    <title>jQuery旋转图片</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jqueryrotate/2.3/jqueryrotate.min.js"></script>
    <style type="text/css">
        .image {
            margin: 20px auto;
            width: 200px;
            height: 200px;
        }
        .image img {
            border: 1px solid #ddd;
        }
        .image .rotate {
            margin-top: 10px;
        }
    </style>
    <script type="text/javascript">
        $(document).ready(function(){
            // 绑定旋转按钮的事件
            $('.rotate').click(function(){
                $('.image img').rotate({
                    angle: 45,
                    animateTo: 90
                });
            });
        });
    </script>
</head>
<body>
    <div class="image">
        <img src="/static/imghwm/default1.png"  data-src="image.jpg"  class="lazy" alt="image" />
        <div class="rotate">
            <input type="button" value="旋转图片" />
        </div>
    </div>
</body>
</html>

After introducing the three files jQuery.js, jQuery-ui.js and jQuery-rotate.js, we can realize the image rotation effect. The specific implementation method is as follows:

1. Add links to the jQuery.js, jQuery-ui.js and jQuery-rotate.js files in the head tag.

2. Define related styles in the style tag, including the styles of pictures and rotation buttons.

3. Define the relevant HTML content of the image in the body tag, including the rotation button and image container.

4. Implement image rotation in the script tag. The specific implementation method is as follows:

(1) Bind the event of the rotation button.

(2)Use jQuery.rotate.js plug-in to achieve image rotation effect.

In the above code, we use the jQuery.rotate.js plug-in to rotate the image. Among them, the .rotate class represents the rotation button, and the .image class represents the image container. When clicking the rotate button, we use the .rotate() function and its parameters to rotate the image. Among them, the angle parameter represents the angle of rotation, and the animateTo parameter represents the time required for the rotation to reach the target angle, in milliseconds.

2. Achieve picture magnification effect

In addition to the rotation effect, the picture magnification effect is also very popular. Next, we will introduce how to use jQuery to achieve the enlargement effect of images.

1. Use CSS3 transform to achieve image magnification effect

The CSS3 transform property can achieve a simple image magnification effect. We can use jQuery's animate() function to achieve dynamic changes in the CSS3 transform attribute. The following is the code to achieve the zoom effect:

html
<!DOCTYPE html>
<html>
<head>
    <title>jQuery放大图片</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <style type="text/css">
        .image {
            margin: 20px auto;
            width: 200px;
            position: relative;
        }
        .image img {
            border: 1px solid #ddd;
        }
        .image .zoom {
            width: 50px;
            height: 50px;
            background-color: #fff;
            position: absolute;
            bottom: 0;
            right: 0;
            border: 1px solid #ddd;
            cursor: pointer;
        }
    </style>
    <script type="text/javascript">
        $(document).ready(function(){
            // 绑定放大按钮的事件
            $('.zoom').click(function(){
                $('.image img').animate({
                    height: '400px'
                }, 500);
            });
        });
    </script>
</head>
<body>
    <div class="image">
        <img src="/static/imghwm/default1.png"  data-src="image.jpg"  class="lazy" alt="image" />
        <div class="zoom"></div>
    </div>
</body>
</html>

Among them, we need to add a zoom container in the image container to store the zoom button. The specific implementation method is as follows:

1. Add a link to the jQuery.js file in the head tag.

2. Define related styles in the style tag, including the styles of pictures and magnification buttons.

3. Define the relevant HTML content of the image in the body tag, including the magnification button and image container.

4. Implement image magnification in the script tag. The specific implementation method is as follows:

(1) Bind the event of the magnification button.

(2) Use jQuery’s animate() function to achieve highly dynamic changes in images.

In the above code, we use the .zoom() function to bind the zoom button. When the button is clicked, we use the animate() function to change the image height from the original 200px to 400px. The animation effect time is 500ms.

2. Use jQuery to enlarge the picture

If you need to achieve a more complex magnification effect, we can use jQuery related plug-ins. The following is the code to achieve the magnification effect:

html
<!DOCTYPE html>
<html>
<head>
    <title>jQuery放大图片</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery-zoom/1.8.13/jquery.zoom.min.js"></script>
    <style type="text/css">
        .image {
            margin: 20px auto;
            width: 200px;
            height: 200px;
        }
        .image img {
            border: 1px solid #ddd;
        }
    </style>
    <script type="text/javascript">
        $(document).ready(function(){
            // 绑定放大按钮的事件
            $('.image img').zoom({
                url: 'image-big.jpg'
            });
        });
    </script>
</head>
<body>
    <div class="image">
        <img src="/static/imghwm/default1.png"  data-src="image-small.jpg"  class="lazy" alt="image" />
    </div>
</body>
</html>

Among them, we use the jquery-zoom.js plug-in to achieve image magnification. The specific implementation method is as follows:

1. Add links to the jQuery.js and jquery-zoom.js files in the head tag.

2. Define related styles in the style tag, including the style of the image.

3. Define the relevant HTML content of the image in the body tag.

4. Use the .zoom() function in the script tag to zoom in on the image. Among them, the url parameter represents the enlarged image path.

In the above code, we use the .zoom() function to achieve the zoom effect of the image. When the mouse wheel is scrolled, the picture will dynamically change, achieving a magnification effect. It should be noted that the picture needs to be a high-definition picture, otherwise the picture quality will be blurred after enlargement.

3. Realize the rotation and magnification effect of the picture

In addition to realizing the rotation and magnification effect of the picture separately, sometimes it is necessary to realize the rotation and magnification effect of the picture at the same time. At this point, we can combine the above two effects. The code is as follows:

html
<!DOCTYPE html>
<html>
<head>
    <title>jQuery旋转放大图片</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jqueryrotate/2.3/jqueryrotate.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery-zoom/1.8.13/jquery.zoom.min.js"></script>
    <style type="text/css">
        .image {
            margin: 20px auto;
            width: 200px;
            height: 200px;
            position: relative;
        }
        .image img {
            border: 1px solid #ddd;
        }
        .image .zoom {
            width: 50px;
            height: 50px;
            background-color: #fff;
            position: absolute;
            bottom: 0;
            right: 0;
            border: 1px solid #ddd;
            cursor: pointer;
        }
        .image .rotate {
            width: 50px;
            height: 50px;
            background-color: #fff;
            position: absolute;
            top: 0;
            left: 0;
            border: 1px solid #ddd;
            cursor: pointer;
        }
    </style>
    <script type="text/javascript">
        $(document).ready(function(){
            // 绑定放大按钮的事件
            $('.image img').zoom({
                url: 'image-big.jpg'
            });

            // 绑定旋转按钮的事件
            $('.rotate').click(function(){
                $('.image img').rotate({
                    angle: 45,
                    animateTo: 90
                });
            });
        });
    </script>
</head>
<body>
    <div class="image">
        <img src="/static/imghwm/default1.png"  data-src="image-small.jpg"  class="lazy" alt="image" />
        <div class="zoom"></div>
        <div class="rotate"></div>
    </div>
</body>
</html>

In the above code, we use the above two effects in combination. The specific implementation method is as follows:

1. Add links to the four files jQuery.js, jquery-ui.js, jquery-rotate.js and jquery-zoom.js in the head tag.

2. Define related styles in the style tag, including the styles of pictures, magnification buttons and rotation buttons.

3. Define the relevant HTML content of the image in the body tag, including zoom and rotate buttons and image containers.

4. Implement the rotation and magnification effect of the image in the script tag. The specific implementation method is as follows:

(1) Use the .zoom() function to achieve the magnification effect of the image.

(2) Bind the .click event of the rotation button.

(3) Use the .rotate() function to achieve the rotation effect of the image.

In the above code, we use jQuery.rotate.js and jquery-zoom.js plug-ins to achieve the image rotation and magnification effects. By binding the .click event of the button, we can control the image. The overall effect is very smooth and can enhance the visual effect of the page.

Summary:

In web design, achieving image rotation and magnification effects has become a very basic requirement. By using jQuery, we can achieve image rotation and magnification effects to enhance the visual effect of the page. In this article, we introduce three implementation methods and give the corresponding code. I believe that through studying this article, readers can already master how to use jQuery to achieve image rotation and magnification effects.

The above is the detailed content of jquery image rotate and enlarge. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
What are the limitations of React?What are the limitations of React?May 02, 2025 am 12:26 AM

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

React's Learning Curve: Challenges for New DevelopersReact's Learning Curve: Challenges for New DevelopersMay 02, 2025 am 12:24 AM

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

Generating Stable and Unique Keys for Dynamic Lists in ReactGenerating Stable and Unique Keys for Dynamic Lists in ReactMay 02, 2025 am 12:22 AM

ThecorechallengeingeneratingstableanduniquekeysfordynamiclistsinReactisensuringconsistentidentifiersacrossre-rendersforefficientDOMupdates.1)Usenaturalkeyswhenpossible,astheyarereliableifuniqueandstable.2)Generatesynthetickeysbasedonmultipleattribute

JavaScript Fatigue: Staying Current with React and Its ToolsJavaScript Fatigue: Staying Current with React and Its ToolsMay 02, 2025 am 12:19 AM

JavaScriptfatigueinReactismanageablewithstrategieslikejust-in-timelearningandcuratedinformationsources.1)Learnwhatyouneedwhenyouneedit,focusingonprojectrelevance.2)FollowkeyblogsliketheofficialReactblogandengagewithcommunitieslikeReactifluxonDiscordt

Testing Components That Use the useState() HookTesting Components That Use the useState() HookMay 02, 2025 am 12:13 AM

TotestReactcomponentsusingtheuseStatehook,useJestandReactTestingLibrarytosimulateinteractionsandverifystatechangesintheUI.1)Renderthecomponentandcheckinitialstate.2)Simulateuserinteractionslikeclicksorformsubmissions.3)Verifytheupdatedstatereflectsin

Keys in React: A Deep Dive into Performance Optimization TechniquesKeys in React: A Deep Dive into Performance Optimization TechniquesMay 01, 2025 am 12:25 AM

KeysinReactarecrucialforoptimizingperformancebyaidinginefficientlistupdates.1)Usekeystoidentifyandtracklistelements.2)Avoidusingarrayindicesaskeystopreventperformanceissues.3)Choosestableidentifierslikeitem.idtomaintaincomponentstateandimproveperform

What are keys in React?What are keys in React?May 01, 2025 am 12:25 AM

Reactkeysareuniqueidentifiersusedwhenrenderingliststoimprovereconciliationefficiency.1)TheyhelpReacttrackchangesinlistitems,2)usingstableanduniqueidentifierslikeitemIDsisrecommended,3)avoidusingarrayindicesaskeystopreventissueswithreordering,and4)ens

The Importance of Unique Keys in React: Avoiding Common PitfallsThe Importance of Unique Keys in React: Avoiding Common PitfallsMay 01, 2025 am 12:19 AM

UniquekeysarecrucialinReactforoptimizingrenderingandmaintainingcomponentstateintegrity.1)Useanaturaluniqueidentifierfromyourdataifavailable.2)Ifnonaturalidentifierexists,generateauniquekeyusingalibrarylikeuuid.3)Avoidusingarrayindicesaskeys,especiall

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.