Home > Web Front-end > Front-end Q&A > jquery image rotate and enlarge

jquery image rotate and enlarge

王林
Release: 2023-05-12 09:45:06
Original
707 people have browsed it

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="image.jpg" alt="image" />
        <div class="rotate">
            <input type="button" value="旋转图片" />
        </div>
    </div>
</body>
</html>
Copy after login

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="image.jpg" alt="image" />
        <div class="zoom"></div>
    </div>
</body>
</html>
Copy after login

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="image-small.jpg" alt="image" />
    </div>
</body>
</html>
Copy after login

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="image-small.jpg" alt="image" />
        <div class="zoom"></div>
        <div class="rotate"></div>
    </div>
</body>
</html>
Copy after login

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!

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