1. Rendering 2. Implementation principleWith 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
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:
|
<<: Get the calculated style in the CSS element (after cascading/final style)
>>: VMware Workstation Pro 16 License Key with Usage Tutorial
Preface: position:sticky is a new attribute of CS...
Summarize This article ends here. I hope it can b...
BMP is an image file format that is independent o...
I once promised that I would keep writing until pe...
One of the most commonly used and discussed data ...
If a form field in a form is set to disabled, the ...
Table of contents 01 Scenario Analysis 02 Operati...
Jenkins is an open source software project. It is...
Original text: https://dev.mysql.com/doc/refman/8...
We know that in general, a function must be calle...
Table of contents Environmental Description Insta...
Preface: As a giant in the IT industry, Microsoft...
What is DOM? With JavaScript, you can reconstruct...
Table of contents Preface On-site investigation C...
<br />For some time, I found that many peopl...