user3261881 user3261881 - 1 year ago 122
HTML Question

Pure JavaScript Image Grab-Zoom?

In the past I came across which was a great find and very simple to use for a variety of images. I am looking for a grab-zoom feature using pure-javascript rather than jQuery. Is there any Pure-Javascript solution for this? I've attempted this with no luck so far:


var imgSize = 1;
function zoomIn() {
imgSize += 0.1; = imgSize + "em";
function zoomOut() {
imgSize -= 0.1; = imgSize + "em";


<img src="picture.png" value="+" onClick="zoomIn()"/>

Thanks in advance for any helpful tips!

Answer Source

The general idea is to display the smaller sized image ("original") and when the mouse is hovering over the container, the background image of the container is revealed. The background image is large, and the CSS background is clipped by the container's size. Mouse events are tracked. You'll have to work out the measurement and math yourself.

<div id="imageview" style="position:relative;width:300px;height:200px;"
  <img src="300x200.jpg" id="original" onmouseover="'none';">



    var x,y;
    if (e) {x=e.clientX;y=e.clientY;} else {x=event.clientX; y=event.clientY;}



    var x,y;
    if (e) {x=e.clientX;y=e.clientY;} else {x=event.clientX; y=event.clientY;}

    var orgx=document.getElementById('imageview').orgx;
    var orgy=document.getElementById('imageview').orgy;

    //now you have x, y, orgx, and org y, so you can do some math
    // ...

    var posx, posy;

    document.getElementById('imageview').backgroundPosition=-1*posx+'px '+ -1*posy+'px';


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download