A Simple Image Zoom

A Simple Image Zoom (Single image zoom without secondary high-res image.)

by Karmik Patel

HTML

<h1>A Simple Image Zoom</h1>

<div class="img-container">
  <div class="img-zoom-lens"></div>
  <img id="my-image" src="http://demos.imgix.net/wristwatch.jpg?w=1000">
  <div id="my-result" class="img-zoom-result"></div>
</div>

<p>Choose more images:</p>
<div class="img-gallery">
    <img src="http://demos.imgix.net/wristwatch.jpg?w=1000">
    <img src="https://assets.imgix.net/unsplash/coyote.jpg?w=1000">
    <img src="https://assets.imgix.net/unsplash/motorbike.jpg?w=1000">
    <img src="https://assets.imgix.net/unsplash/hotairballoon.jpg?w=1000">
</div>

<b>&copy; Created By KaRMiK</b>

CSS

* {
  box-sizing: border-box;
  font-family: sans-serif;
}

.img-container {
  position: relative;
  display: flex;
  justify-content: space-between;
}

.img-zoom-lens {
  position: absolute;
  border: 3px solid rgb(0, 126, 255);
  background-color: rgba(0, 126, 255, .2);
  width: 100px;
  height: 100px;
}

#my-image {
  max-width: 50%;
  height: 300px;
}

.img-zoom-result {
  border: 1px solid #f1f1f1;
  width: 300px;
  height: 300px;
}

.img-gallery {
  margin: 16px 0;
}

.img-gallery img {
  display: inline-block;
  cursor: pointer;
  width: 110px;
  height: 110px;
  border: 1px solid #ccc;
  padding: 4px;
  margin-right: 8px;
}

.img-gallery img:active {
  box-shadow: 0 0 0.1rem 0.1rem rgba(38,143,255,.5);
}

.img-gallery img:focus {
  box-shadow: 0 0 0.1rem 0.1rem rgba(38,143,255,.5);
}

JavaScript

window.addEventListener("load", imageZoom("my-image"));

function imageZoom(imgID) {
    let img, lens, result, cx, cy;
    img = document.getElementById(imgID);
    result = document.getElementById("my-result");
    result.style.visibility = "hidden";
    lens = document.querySelector(".img-zoom-lens");
   // lens.style.visibility = "hidden";
    cx = result.offsetWidth / lens.offsetWidth;
    cy = result.offsetHeight / lens.offsetHeight;
    result.style.backgroundImage = "url('" + img.src + "')";
    result.style.backgroundSize = (img.width * cx) + "px " + (img.height * cy) + "px";
    lens.addEventListener("mousemove", moveLens);
    img.addEventListener("mousemove", moveLens);
    lens.addEventListener("touchmove", moveLens);
    img.addEventListener("touchmove", moveLens);
//KP
    //lens.addEventListener("mouseout", hidezoom);
    img.addEventListener("mouseout", hidezoom);
 		function hidezoom(e) {
    result.style.visibility = "hidden";
    //lens.style.visibility = "hidden";
    }
    function moveLens(e) {
        let pos, x, y;
        result.style.visibility = "visible";
        //lens.style.visibility = "visible";
        e.preventDefault();
        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;}
        lens.style.left = x + "px";
        lens.style.top = y + "px";
        result.style.backgroundPosition = "-" + (x * cx) + "px -" + (y * cy) + "px";
    }
    function getCursorPos(e) {
        let a, x = 0, y = 0;
        e = e || window.event;
        a = img.getBoundingClientRect();
        x = e.pageX - a.left;
        y = e.pageY - a.top;
        x = x - window.pageXOffset;
        y = y - window.pageYOffset;
        return {x : x, y : y};
    }
}

// Function to...