SoFunction
Updated on 2025-02-28

JS realizes the effect of e-commerce magnifying glass

The front-end JS e-commerce magnifying glass effect is for your reference. The specific content is as follows

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>26-E-commerce magnifying glass</title>
  <style type="text/css">
    
  *{
    padding: 0;
    margin: 0;
  }
  #left{
   padding: 0;
  margin: 0;
    width: 400px;
    height: 400px;
    border: 2px solid blue;
    background: url(/t6/17/) no-repeat;
    float: left;
    cursor: crosshair;
    position: relative;
  box-sizing: border-box;
  }
  #box{
    width: 200px;
    height: 200px;
    background: white;
    opacity: 0.6;
    position: absolute;
    top: 0;
    left: 0;
    display: none;
  box-sizing: border-box;
  }
  #cover{
    width: 400px;
    height: 400px;
    background: red;
    position: absolute;
    left: 0;
    top: 0;
    opacity: 0;
  box-sizing: border-box;
  }
  #right{
    width: 400px;
    height: 400px;
    border: 2px solid black;
    overflow: hidden;
    position: relative;
    display: none;
  box-sizing: border-box;
  }
  #rpic{
    position: absolute;
  }
  </style>

  <script type="text/javascript">
    
   = function(){
    var left = ("left");
    var right = ("right");
    var rpic = ("rpic");
    var box = ("box");
    var cover = ("cover");

    // Add a mouse movement event to the left     = function(){

      //Get the event object      var ev = ;
      var mouse_left =  || ;
      var mouse_top =  || ;
      //  = mouse_left + '|' + mouse_top;

      //Calculate the position of the color block      var box_left = mouse_left - 100;
      var box_top = mouse_top - 100;

      // Determine whether it is exceeded      if (box_left < 0) {
        box_left = 0;
      }
      if (box_left > 200) {
        box_left = 200;
      }
      if (box_top < 0) {
        box_top = 0;
      }
      if (box_top > 200) {
        box_top = 200;
      }

      // Let the color blocks move       = box_left + 'px';
       = box_top + 'px';

      //Calculate the picture position on the right      var rpic_left = box_left*-2;
      var rpic_top = box_top*-2;

      // Let the right side move       = rpic_left + 'px';
       = rpic_top + 'px';

    }

      //Add a mouse to move in event on the left side       = function(){
        //Hide the color block on the left and the right         = 'block';
         = 'block';
      }

      // Add a mouse to the left to move out the event       = function(){
        //Hide the color block on the left and the right         = 'none';
         = 'none';
      }
  }

  </script>
</head>
<body>
  <div >
    <div ></div>
    <div ></div>
  </div>
  <div >
    <img src="/t6/17/" >
  </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.