How do I get this zoom script to work properly?

(Markus Bjerre) #1

Hi guys! I have a quick question:
I’m in need of a good zoom function like the one that I have in my project file. However, since I have tons of images that will be loaded into one div I am using a javascript which calls the images to load with a simple button. My problem is that I am not getting the zoom to work with this javascript. It works when I load the image into the div from the beginning, but when I load the image afterwards, the zoom ends up doing nothing. I know this is using external javascript but it would be so cool if you could take a look at it. I’ve made a simple demonstration project for you.

The image to the left (number 1) is working as it should be. The image to the right (number 2) is what I want to work. The button below it will load the image from the javascript.

Any idea?

Project file: (208.8 KB)

Link to the javascript manual:

Many thanks for any help!

Best regards

Markus Bjerre

(Greg) #2

Hi Markus, I think it may have something to do with the innerHTML on the second “div”. The first “div” has…


and the second "div’ has nothing between the quotes…


Maybe you can write [ ${resourcesFolderName}/image2.jpg ] to the innerHTML of the second “div” somehow

Also, I don’t see where you are calling for the “zooming2()” script.

(Markus Bjerre) #3

Hey Greg and thank you for taking time to take a look at the project file! The reason why the second div’s img source is blank is because I don’t want it to preload the image. What I want is the javascript called “showimg2” to load the image instead. It works fine with the button that I have included (even if it’s zoomed in a little bit since the image is bigger than the div, easy fix though with style.backgroundSize). However, if I get the zoom to work AFTER the loading of the image (showImg2), I can easily replace the image in the div with another one and still have the zoom function going on.

My real project is a lot bigger and includes over 30 images linked to a related thumbnail. The div will work like a holder for each thumbnail and it works perfectly with javascript. So the question still remains. How do I get the zoom to work with the javascript that I’m using, or is there any better way to do it? The best way I believe would be that I have a white placeholder and in some way replace the content within the div and still get the zoom working properly. Don’t know how to do that though.

Thanks again Greg for taking your time!

(Markus Bjerre) #4

Solved it after a couple of hours by testing different solutions:
Used this code on trigger the button:

var imagePath = hypeDocument.resourcesFolderURL() + "/image2.png";

hypeDocument.getElementById('imageHolder').src = imagePath;

			$('#contentImage').zoom({ on:'click'});			


And this code I used on the div id called #contentImage:

<img id="imageHolder">

Any idea on how to make a variable out of the image number “2”?

(Jonathan Deutsch) #5

I’m a little unclear on what you mean by making it a variable, but you could probably do something where you have buttons which have unique element ids. Then when those buttons are clicked, you could use the id (say image2, image3, etc.) to place as the .src value.

(Markus Bjerre) #6

Hi Jonathan! If I remember correctly what I wanted is the number in image#.png to be a variable on the top of the code, so I could easily replace the image with var number = 3;

The code would look similar to this:

var number = 3;
var imagePath = hypeDocument.resourcesFolderURL() + "/image" + number + ".png";

I’m not sure how to do it properly though with quotation marks etc.

(Jonathan Deutsch) #7

your code looks good to me; why don’t you try it and see if it works? If there’s a syntax error, you’ll at least get some information in the Developer console.