JavaScript implementation of magnifying glass details

JavaScript implementation of magnifying glass details

1. Rendering

2. Implementation principle

With the help of two pictures with equal width and height enlargement, combined with the mouse offset, element offset, element's own width and height and other attributes in js, it is completed; the mask on the left moves Xpx, and the large picture on the right moves X*multiple px; the rest can be calculated using elementary school mathematics.

HTML and CSS :

 <div class="wrap">
    <!-- Thumbnail and Mask -->
    <div id="small">
      <img src="img/1.jpg" alt="" >
      <div id="mark"></div>
    </div>
    <!-- Enlarged image at the same scale -->
    <div id="big">
      <img src="img/2.jpg" alt="" id="bigimg">
    </div>
  </div>
 * {
      margin: 0;
      padding: 0;
    }
    .wrap {
      width: 1500px;
      margin: 100px auto;
    }

    #small {
      width: 432px;
      height: 768px;
      float: left;
      position: relative;
    }

    #big {
      /* background-color: seagreen; */
      width: 768px;
      height: 768px;
      float: left;
      /* The part beyond the viewfinder is hidden*/
      overflow: hidden;
      margin-left: 20px;
      position: relative;
      display: none;
    }

    #bigimg {
      /* width: 864px; */
      position: absolute;
      left: 0;
      top: 0;
    }

    #mark {
      width: 220px;
      height: 220px;
      background-color: #fff;
      opacity: .5;
      position: absolute;
      left: 0;
      top: 0;
      /* Mouse arrow style*/
      cursor: move;
      display: none;
    }

// Get the small image and mask, large image, and large box var small = document.getElementById("small")
    var mark = document.getElementById("mark")
    var big = document.getElementById("big")
    var bigimg = document.getElementById("bigimg")
    // Get mouse movement events in the small image area; the mask follows the mouse movement small.onmousemove = function (e) {
      // Get the offset of the mask relative to the thumbnail (mouse coordinates - thumbnail offset relative to body - half of the width or height of the mask)
      var s_left = e.pageX - mark.offsetWidth / 2 - small.offsetLeft
      var s_top = e.pageY - mark.offsetHeight / 2 - small.offsetTop
      // The mask can only be moved within the thumbnail, so the threshold value of the mask offset (relative to the thumbnail value) needs to be calculated
      var max_left = small.offsetWidth - mark.offsetWidth;
      var max_top = small.offsetHeight - mark.offsetHeight;
      // When the mask moves, the large image on the right also moves (for every 1px the mask moves, the image needs to move n times the distance in the opposite direction)
      var n = big.offsetWidth / mark.offsetWidth
      // Judge before the mask follows the mouse movement: the offset of the mask relative to the thumbnail cannot exceed the range, and it needs to be reassigned if it exceeds the range (the critical value has been calculated above: max_left and max_top)
      // Determine the horizontal boundary if (s_left < 0) {
        s_left = 0
      } else if (s_left > max_left) {
        s_left = max_left
      }
      //Judge the vertical boundary if (s_top < 0) {
        s_top = 0
      } else if (s_top > max_top) {
        s_top = max_top
      }
      // Assign values ​​to the mask left and top (dynamically? Because e.pageX and e.pageY are changing quantities), move it!
      mark.style.left = s_left + "px";
      mark.style.top = s_top + "px";
      // Calculate the distance the large image moves var levelx = -n * s_left;
      var verticaly = -n * s_top;
      // Make the picture move bigimg.style.left = levelx + "px";
      bigimg.style.top = verticaly + "px";
    }
    // The mask and follow-up style will be displayed only when the mouse moves into the small picture, and will disappear when the mouse moves out of the small picture small.onmouseenter = function () {
      mark.style.display = "block"
      big.style.display="block"
    }
    small.onmouseleave = function () {
      mark.style.display = "none"
      big.style.display="none"
    }

3. Summary

  • Once the mouse focus moves, its offset is dynamic; after the parent element and child elements are positioned, the "dynamic" effect is achieved by dynamically changing left and top values ​​of an element.
  • Large image/small image = magnifying glass (mask)/viewfinder
  • The two images must be scaled in equal proportions.

This is the end of this detailed article about how to implement a magnifying glass in js. For more relevant content about how to implement a magnifying glass in js, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Implementation process of the magnifying glass effect in the Javascript example project
  • JavaScript realizes magnifying glass special effects
  • JavaScript imitates Jingdong magnifying glass special effects
  • JavaScript object-oriented implementation of magnifying glass case
  • JavaScript imitates Jingdong magnifying glass effect
  • JavaScript imitates Taobao magnifying glass effect
  • JavaScript to achieve magnifying glass effect
  • Ideas and codes for realizing magnifying glass effect in js

<<:  Get the calculated style in the CSS element (after cascading/final style)

>>:  VMware Workstation Pro 16 License Key with Usage Tutorial

Recommend

Detailed explanation of CSS sticky positioning position: sticky problem pit

Preface: position:sticky is a new attribute of CS...

Detailed explanation of Vue's ref attribute

Summarize This article ends here. I hope it can b...

Detailed explanation of the benefits of PNG in various network image formats

BMP is an image file format that is independent o...

How to implement a binary search tree using JavaScript

One of the most commonly used and discussed data ...

How to submit the value of a disabled form field in a form Example code

If a form field in a form is set to disabled, the ...

How to clean up data in MySQL online database

Table of contents 01 Scenario Analysis 02 Operati...

A detailed tutorial on how to install Jenkins on Docker for beginners

Jenkins is an open source software project. It is...

Briefly describe mysql monitoring group replication

Original text: https://dev.mysql.com/doc/refman/8...

JavaScript immediate execution function usage analysis

We know that in general, a function must be calle...

Hyper-V Introduction and Installation and Use (Detailed Illustrations)

Preface: As a giant in the IT industry, Microsoft...

Introduction to HTML DOM_PowerNode Java Academy

What is DOM? With JavaScript, you can reconstruct...

MySQL data loss troubleshooting case

Table of contents Preface On-site investigation C...

Summarize the common application problems of XHTML code

<br />For some time, I found that many peopl...