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

JavaScript Function Currying

Table of contents 1 What is function currying? 2 ...

Vue-cli creates a project and analyzes the project structure

Table of contents 1. Enter a directory and create...

js native carousel plug-in production

This article shares the specific code for the js ...

Implementing calculator functions with WeChat applet

This article is a simple calculator written using...

How does Zabbix monitor and obtain network device data through ssh?

Scenario simulation: The operation and maintenanc...

Sample code for implementing rolling updates of services using Docker Swarm

1. What is Docker Swarm? Docker Swarm is a cluste...

Example of adding and deleting range partitions in MySQL 5.5

introduce RANGE partitioning is based on a given ...

jQuery implements form validation

Use jQuery to implement form validation, for your...

JavaScript implements a box that follows the mouse movement

This article shares the specific code of JavaScri...