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

Why not use UTF-8 encoding in MySQL?

MySQL UTF-8 encoding MySQL has supported UTF-8 si...

Detailed tutorial on installing Mysql5.7.19 on Centos7 under Linux

1. Download MySQL URL: https://dev.mysql.com/down...

How to connect to MySQL using C++

C++ connects to MySQL for your reference. The spe...

Window.name solves the problem of cross-domain data transmission

<br />Original text: http://research.microso...

Installation process of zabbix-agent on Kylin V10

1. Download the installation package Download add...

Detailed explanation of the use of Teleport in Vue3

Table of contents Purpose of Teleport How Telepor...

How to fix the width of table in ie8 and chrome

When the above settings are used in IE8 and Chrome...

Some "pitfalls" of MySQL database upgrade

For commercial databases, database upgrade is a h...

MySQL optimization query_cache_limit parameter description

query_cache_limit query_cache_limit specifies the...

Upgrade Docker version of MySQL 5.7 to MySQL 8.0.13, data migration

Table of contents 1. Back up the old MySQL5.7 dat...

The difference between ENTRYPOINT and CMD in Dockerfile

In the Docker system learning tutorial, we learne...

Summary of Linux sftp command usage

sftp is the abbreviation of Secure File Transfer ...

MySQL derived table (Derived Table) simple usage example analysis

This article uses an example to describe the simp...

Analysis on the problem of data loss caused by forced refresh of vuex

vuex-persistedstate Core principle: store all vue...