How To Create Zoom Image Detail

How To Create Zoom Image Detail

Hello guys, in this post I will discuss a bit about the Zooming program which the program will display a picture in the browser and when the cursor is highlighted in the image area it will be displayed directly (Zoom In) or more detail the area of ​​the image.

Okay just start our tutorial, now open your text editor then follow the following code, and here I use the Sublime text editor:

 <title>Zoom Image</title>
* {box-sizing:border-box;}
 position: relative;
 position: absolute;
 border: 1px solid #d4d4d4;
 width: 40px;
 height: 40px;

 border: 1px solid #000;
 width: 300px;
 height: 300px;
function imagezoom(imgID, resultID){
 var img, lens, result, cx, cy;
 img = document.getElementById(imgID);
 result = document.getElementById(resultID);
 lens = document.createElement("DIV");
 lens.setAttribute("class", "img-zoom-lens");
 img.parentElement.insertBefore(lens, img);
 cx = result.offsetWidth / lens.offsetWidth;
 cy = result.offsetHeight / lens.offsetHeight; = "url('" + img.src + "')"; = (img.width * cx) + "px " + (img.height * cy) + "px";
 lens.addEventListener("mousemove", moveLens);
 img.addEventListener("mousemove", moveLens);

 lens.addEventListener("touchmove", moveLens);
 img.addEventListener("touchmove", moveLens);

 function moveLens(e){
  var pos, x, y;
  pos = getCursorPos(e);
  x= pos.x - (lens.offsetWidth / 2);
  y = pos.y - (lens.offsetHeight / 2);

  if(x > img.width - lens.offsetWidth){x = img.width - lens.offsetWidth;}
  if(x < 0){x=0;}
  if(y > img.height - lens.offsetHeight){y = img.height - lens.offsetHeight;}
  if(y < 0){y=0;} = x + "px"; = y + "px"; = "-" + (x * cx) +"px -" + (y * cy) + "px";     

 function getCursorPos(e){
  var a, x = 0, y = 0;
  e = e || window.event;
  a = img.getBoundingClientRect();
  x = e.pageX - a.left;
  y = e.pageY -;

  x = x - window.pageXOffset;
  y = y - window.pageYOffset;
  return {x : x, y : y};


Code above for tag <head></head> which includes your Title and CSS .HTML files

For Tag <body></body> enter code below

Zoom Image

<p>Mouse Over The Image</p>
<div class="img-zoom-container">
 <img id="myimage" src="img.png" width="300" height="200" border="1px">   
 <div id="myresult" class="img-zoom-result"></div>
imagezoom("myimage", "myresult");


Make sure you've set the image and save it in a folder with this .HTML file, and customize its name in the src tag
If all is done, immediately save with type .HTML then run through your browser.

For more details, please watch the video below

No comments:

Powered by Blogger.