Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

javascript - The image zoom magnifier effect does not work properly in Angular

I created an image zoom magnifier effect in angular9. I followed this link of all code. https://codepen.io/piclaunch/pen/QrLWNx

When I run my application, it's magnifier zoom effect does nothing like the above runnable code demonstration.

Here is my code:-

<app-home></app-home>
<br/><br/>

<style>
 @media only screen and (min-width: 600px) {
* {box-sizing: border-box;}
.img-zoom-container {
  position: relative;
}
.img-zoom-lens {
  position: absolute;
  border: 1px solid #d4d4d4;
  /*set the size of the lens:*/
  width: 75px;
  height: 75px;
}
.img-zoom-result {
  border: 1px solid #d4d4d4;
  /*set the size of the result div:*/
  width: 300px;
  height: 300px;
}



#myhide {
    display: none;
}
.img-zoom-container:hover #myhide {
    display:block;
}
}

@media only screen and (max-width: 600px) {
    .img-zoom-container:hover #myhide {
    display:none;
}
  .img-zoom-container {
  position: relative;
  width: 480px;
  height: 320px;
  overflow: hidden;
}

.imgid {
  position: absolute;
  top: 0;
  left: 0;
}

.imgid img {
  -webkit-transition: 0.6s ease;
  transition: 0.6s ease;
}

.img-zoom-container:hover .imgid img {
  -webkit-transform: scale(1.9);
  transform: scale(1.9);
  overflow:show;
}
  
}

</style>

<div class="row container mt-5 mr-5">
  <div class="col-3 mr-2">
  //other code
  </div>
<div class="col-3">
  <div class="img-zoom-container" onmousenter="showme(this)">
  
        <span><p class="imgid" style="align:center;"><img id="myimage" src="./assets/Images/1.jpg"  srcset="./assets/Images/1.jpg" width="300" height="200"></p></span>
        <span id="myhide" style="float: right;
        position: absolute;
        top: -100px;
        left: 300px;
        width: auto;
        height: 100%;">
        <div id="myresult" class="img-zoom-result"  onmouseleave="hideme(this)" > 
 </div></span>
    </div>

 </div>

<div class="col-6">
   
  //other code
    
 </div>
</div>

<script>
    imageZoom("myimage", "myresult");
  function imageZoom(imgID, resultID) {
    var img, lens, result, cx, cy;
    img = document.getElementById(imgID);
    result = document.getElementById(resultID);
    /*create lens:*/
    lens = document.createElement("DIV");
    lens.setAttribute("class", "img-zoom-lens");
    /*insert lens:*/
    img.parentElement.insertBefore(lens, img);
    /*calculate the ratio between result DIV and lens:*/
    console.log("result.offsetWidth  >>>>>", result.offsetWidth ,"lens.offsetWidth>>>>>>>>>>>",lens.offsetWidth);
  cx = 300 / lens.offsetWidth;
  console.log("result.offsetHeight>>>>>",result.offsetHeight ,"llens.offsetHeighth>>>>>>>>>>>",lens.offsetHeight);
  cy = 300 / lens.offsetHeight;
  /*set background properties for the result DIV:*/
  result.style.backgroundImage = "url('" + img.srcset + "')";
  result.style.backgroundSize = (img.width * cx) + "px " + (img.height * cy) + "px";
  /*execute a function when someone moves the cursor over the image, or the lens:*/
  lens.addEventListener("mousemove", moveLens);
  img.addEventListener("mousemove", moveLens);
  /*and also for touch screens:*/
  lens.addEventListener("touchmove", moveLens);
  img.addEventListener("touchmove", moveLens);
 // img.addEventListener("mouseenter", bigImg);  
 
  
  
  function bigImg(x) {
    console.log("onmouseenter >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
}

function normalImg(x) {
//result.style.display ="none";
     console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>onmousLEAVE");
}


  
  function moveLens(e) {
    var pos, x, y;
    /*prevent any other actions that may occur when moving over the image:*/
    e.preventDefault();
    /*get the cursor's x and y positions:*/
    pos = getCursorPos(e);
    /*calculate the position of the lens:*/
    x = pos.x - (lens.offsetWidth / 2);
    y = pos.y - (lens.offsetHeight / 2);
  // console.log("x" , x , "and Y " , y); 
    /*prevent the lens from being positioned outside the image:*/
    if (x > img.width - lens.offsetWidth) {x = img.width - lens.offsetWidth;  } //else{img.addEventListener("mouseenter", bigImg);  }
    if (x < 0) {x = 0;}
    if (y > img.height - lens.offsetHeight) {y = img.height - lens.offsetHeight; img.addEventListener("mouseleave",  normalImg);}//else{img.addEventListener("mouseenter", bigImg);  }
    if (y < 0) {y = 0;}
    /*set the position of the lens:*/
    lens.style.left = x + "px";
    lens.style.top = y + "px";
    /*display what the lens "sees":*/
    result.style.backgroundPosition = "-" + (x * cx) + "px -" + (y * cy) + "px";
  }
  function getCursorPos(e) {
    var a, x = 0, y = 0;
    e = e || window.event;
    /*get the x and y positions of the image:*/
    a = img.getBoundingClientRect();
    //console.log("------------------A  left" ,  a ); 
    /*calculate the cursor's x and y coordinates, relative to the image:*/
    x = e.pageX - a.left;
    y = e.pageY - a.top;
    /*consider any page scrolling:*/
    x = x - window.pageXOffset;
    y = y - window.pageYOffset;
    return {x : x, y : y};
  }
  
}

function hideme(x) {
    //x.style.display = "none";
   
}
function showme(x) {
    //x.style.display = "block";
   
}



</script>




 <router-outlet></router-outlet>

When I hover the mouse, I find a shadow border on the right side. But this is unexpected. I don't want box-shadow.

question from:https://stackoverflow.com/questions/65713927/the-image-zoom-magnifier-effect-does-not-work-properly-in-angular

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...