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.
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};
}
}
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.
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?
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.
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
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 ?