JavaScript imitates Jingdong magnifying glass effect

JavaScript imitates Jingdong magnifying glass effect

This article shares the specific code for JavaScript to achieve the Jingdong magnifying glass effect for your reference. The specific content is as follows

Case Study

  • The entire case can be divided into three functional modules
  • When the mouse passes over the small picture box, the yellow mask layer and the large picture box are displayed, and the function of hiding the two boxes is left.
  • The yellow occlusion layer follows the mouse function.
  • Move the yellow masking layer and the large image will follow the movement.
  • When the mouse passes over the small picture box, the yellow mask layer and the large picture box are displayed, and the function of hiding the two boxes is left.
  • Show and hide

  • Move the yellow occlusion layer, and the large image will follow the movement. The moving distance of the large image should be equal to the ratio of the yellow occlusion layer.
  • Find the formula for moving the large image

Code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .preview_wrap {
            width: 400px;
            height: 400px;
        }

        .preview_img {
            position: relative;
            height: 398px;
            border: 1px solid #ccc;
        }

        .mask {
            display: none;
            position: absolute;
            top: 0;
            left: 0;
            width: 300px;
            height: 300px;
            background: #FEDE4F;
            opacity: .5;
            border: 1px solid #ccc;
            cursor: move;
        }

        .big {
            display: none;
            position: absolute;
            left: 410px;
            top: 0;
            width: 500px;
            height: 500px;
            background-color: pink;
            z-index: 999;
            border: 1px solid #ccc;
            overflow: hidden;
        }

  /* You need to add absolute positioning to the image before you can set left top */
        .bigimg {
            position: absolute;
            top: 0;
            left: 0;
        }
    </style>
</head>
<body>
    <div class="preview_wrap">
        <div class="preview_img">
            <img src="images/s3.png" alt="">
            <div class="mask"></div>
            <div class="big">
                <img src="images/big.jpg" alt="" class="bigImg">
            </div>
        </div>
    </div>
    <script>
     var preview_img = document.querySelector('.preview_img');
     var mask = document.querySelector('.mask');
     var big = document.querySelector('.big');
     // 1. When our mouse passes over preview_img, we show and hide the mask layer and the big box preview_img.addEventListener('mouseover', function() {
         mask.style.display = 'block';
         big.style.display = 'block';
     })
     preview_img.addEventListener('mouseout', function() {
             mask.style.display = 'none';
             big.style.display = 'none';
         })
         // 2. When the mouse moves, let the yellow box follow the mouse preview_img.addEventListener('mousemove', function(e) {
         // (1). First calculate the coordinates of the mouse in the box var x = e.pageX - this.offsetLeft;
         var y = e.pageY - this.offsetTop;
         // console.log(x, y);
         // (2) Subtract half of the box height 300 to get 150, which is the final left and top values ​​of our mask // (3) The distance our mask moves var maskX = x - mask.offsetWidth / 2;
         var maskY = y - mask.offsetHeight / 2;
         // (4) If the x coordinate is less than 0, let it stop at 0 // Maximum moving distance of the occlusion layer var maskMax = preview_img.offsetWidth - mask.offsetWidth;
         if (maskX <= 0) {
             maskX = 0;
         } else if (maskX >= maskMax) {
             maskX = maskMax;
         }
         if (maskY <= 0) {
             maskY = 0;
         } else if (maskY >= maskMax) {
             maskY = maskMax;
         }
         mask.style.left = maskX + 'px';
         mask.style.top = maskY + 'px';
         // 3. Moving distance of big picture = moving distance of occluding layer * maximum moving distance of big picture / maximum moving distance of occluding layer // Big picture var bigIMg = document.querySelector('.bigImg');
         // Maximum moving distance of large image var bigMax = bigIMg.offsetWidth - big.offsetWidth;
         // Moving distance XY of the large image
         var bigX = maskX * bigMax / maskMax;
         var bigY = maskY * bigMax / maskMax;
         bigIMg.style.left = bigX + 'px';
         bigIMg.style.top = bigY + 'px';
 
     })
    </script>
</body>
</html>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • JavaScript imitates Jingdong magnifying glass special effects
  • js imitates Jingdong magnifying glass effect
  • JavaScript implements Jingdong magnifying glass effect
  • Pure js imitates Taobao Jingdong product magnifying glass function
  • Javascript imitates the effect of Jingdong magnifying glass
  • Using JavaScript to implement Jingdong magnifying glass effect

<<:  Pure CSS to achieve horizontal line animation under the element (background-image)

>>:  Solution to Docker image downloading too slowly

Recommend

MySQL pessimistic locking and optimistic locking implementation

Table of contents Preface Actual Combat 1. No loc...

Centos7 install mysql5.6.29 shell script

This article shares the shell script of mysql5.6....

React+TypeScript project construction case explanation

React project building can be very simple, but if...

The difference between Readonly and Disabled

To summarize: Readonly is only valid for input (te...

Pure JS method to export table to excel

html <div > <button type="button&qu...

Specific use of MySQL segmentation function substring()

There are four main MySQL string interception fun...

Util module in node.js tutorial example detailed explanation

Table of contents Starting from type judgment Str...

How to write the parent and child directories of HTML relative paths

How to indicate the parent directory ../ represent...

Perfect Solution for No rc.local File in Linux

Newer Linux distributions no longer have the rc.l...

Linux Check the installation location of the software simple method

1. Check the software installation path: There is...

How to build sonarqube using docker

Table of contents 1. Install Docker 2. Install so...

Detailed explanation of JavaScript error capture

Table of contents 1. Basic usage and logic 2. Fea...

Detailed explanation of the use of Vue mixin

Table of contents Use of Vue mixin Data access in...

5 tips for writing CSS to make your style more standardized

1. Arrange CSS in alphabetical order Not in alphab...