This article shares the specific code for JavaScript to achieve the magnifying glass effect for your reference. The specific content is as follows The effect achieved this time is as follows: This case is a little bit difficult, and it requires more thinking on both CSS and JS. Without further ado, let's get started~ 1. First, we need to use HTML and CSS to plan the overall layout , that is, two adjacent boxes A and B, and a small box S in the left box A. In order to achieve adjacency, the method I adopted was to set 2. Then we need to use js to set the animation effect , that is: when the mouse is placed on box A, the position of small box S will move with the movement of the mouse, and the image in box B will become an enlarged version of the image covered by box S. How to achieve it? 3. First , realize the position change of small box S: call the 4. Then make the image in box B become an enlarged version of the image covered by box S: Let’s first think about a question, how can this enlargement effect be achieved? From my implementation point of view, for box B, it first needs a background image == the image in box A, and then enlarge it by a certain multiple x. When box S moves, change 5. Finally , what are the values of x and y? (Assuming S, A, and B are all in equal proportions) x : The factor by which box B is enlarged should be equal to the size of A divided by the size of S, so that the same image range can be achieved. y : The change in distance when B moves should be the distance box S moves * (the size of box B divided by the size of S). You can think more~ Maybe my implementation process is complicated. If you think of a better way, please leave a message. The code is as follows: <!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> * { margin: 0; padding: 0; } #box { position: absolute; left: 180px; top: 100px; } #box img { width: 400px; height: 300px; border: 1px solid rgba(255, 255, 255, 0.7); vertical-align: bottom; } #nav { width: 480px; height: 360px; border: 1px solid rgba(255, 255, 255, 0.7); position: absolute; left: 582px; top: 100px; background-image: url(../img/morn.jpg); background-repeat: no-repeat; background-size: 250% 250% } #small { width: 160px; height: 120px; position: absolute; } </style> </head> <body> <div id="box"> <div id="small"></div> <img src="../img/morn.jpg" alt=""> </div> <div id="nav"></div> <script> let box = document.getElementById("box"); let small = document.getElementById("small"); let nav = document.getElementById("nav"); box.onmousemove = function (client) { let x = client.clientX - box.offsetLeft; let y = client.clientY - box.offsetTop; small.style.left = x - 80 + 'px'; small.style.top = y - 60 + 'px'; let dis_x = box.offsetLeft + box.offsetWidth - client.clientX; let dis_y = box.offsetTop + box.offsetHeight - client.clientY; nav.style.backgroundPositionX = (80 - x) * 3 + 'px'; nav.style.backgroundPositionY = (60 - y) * 3 + 'px'; if (x - 80 < 0) { small.style.left = 0; nav.style.backgroundPositionX = 0; } if (dis_x <= 80) { small.style.left = box.offsetWidth - 160 + 'px'; nav.style.backgroundPositionX = (160 - box.offsetWidth) * 3 + 'px'; } if (y - 60 < 0) { small.style.top = 0; nav.style.backgroundPositionY = 0; } if (dis_y < 60) { small.style.top = box.offsetHeight - 120 + 'px'; nav.style.backgroundPositionY = (120 - box.offsetHeight) * 3 + 'px'; } small.style.backgroundColor = "rgba(135, 207, 235, 0.61)"; } box.onmouseout = function () { small.style.backgroundColor="transparent" } </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:
|
<<: W3C Tutorial (15): W3C SMIL Activities
>>: Solution to the problem of invalid line-height setting in CSS
1. Differences between JSON.stringify() and JSON....
1. Background The company's projects have alw...
Table of contents Installation package download I...
Excel is the most commonly used tool for data ana...
In Vue, we can define (register) local components...
Table of contents this Method In the object Hidde...
Table of contents Preface 1. How to write functio...
If there is an <input type="image">...
1. Introduction Are you still leaving your websit...
When it comes to <fieldset> and <legend&...
This article shows you how to use CSS to create a...
COALESCE is a function that refers to each parame...
Experimental environment A minimally installed Ce...
Table of contents In JavaScript , we can usually ...
Table of contents Defining the HTML structure Inp...