Magnify effect on image (js, css)

I have a js/css routine that I’d like to add to an image. When mouse (or touch, if someone is able to adapt the code) hovers the image, a circular magnified portion is displayed.

Enclose is the full code :
Loupe OK.zip (1.3 MB)

css is

* {box-sizing: border-box;}

.img-magnifier-container {
  position: relative;
}

.img-magnifier-glass {
  position: absolute;
  border: 1px solid #000;
  border-radius: 50%;
  cursor: none;
  /*Set the size of the magnifier glass:*/
  width: 200px;
  height: 200px;
}

js is

function magnify(imgID, zoom) {
  var img, glass, w, h, bw;
  img = document.getElementById(imgID);

  /* Create magnifier glass: */
  glass = document.createElement("DIV");
  glass.setAttribute("class", "img-magnifier-glass");

  /* Insert magnifier glass: */
  img.parentElement.insertBefore(glass, img);

  /* Set background properties for the magnifier glass: */
  glass.style.backgroundImage = "url('" + img.src + "')";
  glass.style.backgroundRepeat = "no-repeat";
  glass.style.backgroundSize = (img.width * zoom) + "px " + (img.height * zoom) + "px";
  bw = 3;
  w = glass.offsetWidth / 2;
  h = glass.offsetHeight / 2;

  /* Execute a function when someone moves the magnifier glass over the image: */
  glass.addEventListener("mousemove", moveMagnifier);
  img.addEventListener("mousemove", moveMagnifier);

  /*and also for touch screens:*/
  glass.addEventListener("touchmove", moveMagnifier);
  img.addEventListener("touchmove", moveMagnifier);
  function moveMagnifier(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);
    x = pos.x;
    y = pos.y;
    /* Prevent the magnifier glass from being positioned outside the image: */
    if (x > img.width - (w / zoom)) {x = img.width - (w / zoom); }
    if (x < w / zoom) {x = w / zoom;}
    if (y > img.height - (h / zoom)) {y = img.height - (h / zoom);}
    //if (y < h / zoom) {y = h / zoom;}
        if (y < h / zoom) {element = document.getElementByClassName('img-magnifier-glass'); element.remove();}
    /* Set the position of the magnifier glass: */
    glass.style.left = ((x+img.x) - w) + "px";
    glass.style.top = (y - h) + "px";
    /* Display what the magnifier glass "sees": */
    glass.style.backgroundPosition = "-" + ((x * zoom) - w + bw) + "px -" + ((y * zoom) - h + bw) + "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();
    /* 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};
  }
}

Syntax is

<div class="img-magnifier-container">
  <img id="myimage" src="Image.jpg" style="display: block; margin-left: auto; margin-right: auto" width="600" height="400" justify="center">
</div>

I’m on my phone. But a quick skim suggests you have nothing calling the function magnify

You can ditch the * style in the CSS, you won’t need that.

You can just use the image bric. You don’t need to add the html markup. Just remember to add the class.

I would get an array of all the images you wanted on the page to have this effect (give it a class) and add a hover event listener to each one.

2 Likes

Thank you !
You’re right, there’s a script in the code initialising the function :

question are :

  • where should I put the function calling script ?
  • I embedded the css and js file in project parameter > attached files. Is It ok ?
  • Class should be added to the img or to the div embedding the img ?

Use the code editor and input zones.

CSS - additional CSS zone
JavaScript - Footer (inside script tags)

Inspect the HTML blocs creates for the image bric and compare to the markup you have. You will be able to figure it out. You can adjust the code to suit your needs.

Add the event listener for hovering over images and let that call the function.

I would change the code to remove the need for IDs as if you are using an event listener on each one, it can just be relative to the elements you hover. Otherwise you need a ID for each one.

I’m not sure, but you probably need to destroy the mouse event listeners on mouseoff? Have a play.

1 Like

This doesn’t do much when adding this to CodePen, But maybe does when you run it live on the site.
By the way are there any platforms that allow you to run HTML, CSS and JS live that actually work?

As above Alan. The function is not being called. So nothing happens.

You already mention CodePen that’s what it does. Or are you talking about page builders?

any other live testing platforms, beside CodePen

Okay what’s the point of the code then? :confused:

The purpose of the thread was to get it working.

Create your own local one. But yes there are other website similar to CodePen.

Input = Output. The entire purpose of these platforms is testing and development.

Codepen, JSFiddle and multitude of others like them all work, so as long as a person knows what they are doing. If something across the HTML / CSS / JS is not properly setup things will fail or falter, that should be easily understood. This appears to be where the code was derived from, so learn from there.

2 Likes

Copied and pasted all the code in CodePen and no magnifying glass appears.

Works fine for me.

:wink:

2 Likes

It won’t let me like this post twice :joy:

Hi Jerry,
I search and did find your post about it but saw nowhere to download the code. Could you send a link please ? Thanks

Hi Jerry, it’s exactly what I’m trying to achieve. Where can I download a Blocs document for it ?

After you deposit $300 to @Jerry Swiss based PayPal account. :joy::grin:

1 Like

Sorry, I’m new here. Is there anything I should know ?

Thanks Jerry.
But more seriously, would you accept to share your magnifying module ?

All jokes aside, what Pete and Jerry are trying to do, is to encourage you (and everybody else) to try things and learn to use the pieces and advices they shared to solve your issue yourself. I still manage to resist everything related to learning JS though. Makes my brain hurt.

But I guess if Jerry manages to learn JS, I can too :stuck_out_tongue:

1 Like

Oh, lovely, thanks.
Well, be sure that if I’m here asking for assistance is that I tried, read between the cryptic lines and follow what I understood from advices but wasn’t able to achieve anything. It’s ok for me to understand the basic JS code I mentioned in my first post but I’m not yet familiar enough with the Blocs subtilities to make it work. So, again, help would be great. Choosing Blocs is precisely because my programming skills are not at their best.
Question :

  • The html/js/css code works on my html editor but to work in a browser, it needs to be uploaded. If I try to implement it to Blocs, will it work from the preview or will I have to upload it ?