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 is useEffect? How do you use it to perform side effects?What is useEffect? How do you use it to perform side effects?Mar 19, 2025 pm 03:58 PM

The article discusses useEffect in React, a hook for managing side effects like data fetching and DOM manipulation in functional components. It explains usage, common side effects, and cleanup to prevent issues like memory leaks.

Explain the concept of lazy loading.Explain the concept of lazy loading.Mar 13, 2025 pm 07:47 PM

Lazy loading delays loading of content until needed, improving web performance and user experience by reducing initial load times and server load.

What are higher-order functions in JavaScript, and how can they be used to write more concise and reusable code?What are higher-order functions in JavaScript, and how can they be used to write more concise and reusable code?Mar 18, 2025 pm 01:44 PM

Higher-order functions in JavaScript enhance code conciseness, reusability, modularity, and performance through abstraction, common patterns, and optimization techniques.

How does currying work in JavaScript, and what are its benefits?How does currying work in JavaScript, and what are its benefits?Mar 18, 2025 pm 01:45 PM

The article discusses currying in JavaScript, a technique transforming multi-argument functions into single-argument function sequences. It explores currying's implementation, benefits like partial application, and practical uses, enhancing code read

How does the React reconciliation algorithm work?How does the React reconciliation algorithm work?Mar 18, 2025 pm 01:58 PM

The article explains React's reconciliation algorithm, which efficiently updates the DOM by comparing Virtual DOM trees. It discusses performance benefits, optimization techniques, and impacts on user experience.Character count: 159

How do you prevent default behavior in event handlers?How do you prevent default behavior in event handlers?Mar 19, 2025 pm 04:10 PM

Article discusses preventing default behavior in event handlers using preventDefault() method, its benefits like enhanced user experience, and potential issues like accessibility concerns.

What is useContext? How do you use it to share state between components?What is useContext? How do you use it to share state between components?Mar 19, 2025 pm 03:59 PM

The article explains useContext in React, which simplifies state management by avoiding prop drilling. It discusses benefits like centralized state and performance improvements through reduced re-renders.

What are the advantages and disadvantages of controlled and uncontrolled components?What are the advantages and disadvantages of controlled and uncontrolled components?Mar 19, 2025 pm 04:16 PM

The article discusses the advantages and disadvantages of controlled and uncontrolled components in React, focusing on aspects like predictability, performance, and use cases. It advises on factors to consider when choosing between them.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft