SoFunction
Updated on 2025-02-28

JS realizes magnifying glass effect

JS realizes the effect of magnifying glass for your reference. The specific content is as follows

Move the mouse to the picture to enlarge an area

Code:

<!DOCTYPE html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
   #small{
    width: 700px;
    height: 340px;
    position: absolute;
    left: 70px;
    top: 100px;
   }
   #small img{
    width: 100%;
    height: 100%;
   }
   #mark{
    width: 200px;
    height: 200px;
    background-color:white;
    position: absolute;
    opacity: 0.5;
    filter: alpha(opacity=50);
    left: 0px;
    top: 0px;
    display: none;
   }
   #big{
    width: 400px;
    height: 400px;
    border: 1px solid black;
    left: 800px;
    top: 100px;
    position: absolute;
    overflow: hidden;
    display: none;
   }
   #big img{
    width: 1400px;
    height: 680px;
    position: absolute;
    left: 0px;
    top: 0px;
   }
  </style>
  <script>
    = function(){
    var oSmall = ("small");
    var oBig = ("big");
    var oMark = ("mark");
    var oBigImg = ("img")[0];

     = function(){
      = "block";
      = "block";
    }
     = function(){
      = "none";
      = "none";
    }
     = function(ev){
     var e = ev || ;
     var l =  -  - 100;
     if(l <= 0){
      l = 0;
     }
     if(l >= (700-200)){
      l = 500;
     }
     var t =  -  - 100;
     if(t <= 0){
      t = 0;
     }
     if(t >= (340-200)){
      t = 140;
     }
      = l + 'px';
      = t + 'px';

     /* The moving direction of the picture on the right is opposite to the picture on the left and moves exponentially */
      = l * -2 + 'px';
      = t * -2 + 'px';

    }
   }
  </script>
 </head>
 <body>
  <div >
   <img src="Hell's Gate Guardian Galio.jpg" alt="What's going on">
   <div ></div>
  </div>
  <div >
   <img src="Hell's Gate Guardian Galio.jpg" alt="What's going on">
  </div>
 </body>
</html>

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.