Below is a how-to on setting up your record's photos to pop-up in a beautiful lightbox view and gallery. Nic suggested I share this and now that I have all the kinks worked out, here you go!
Here is a demo site that shows the end-product
(opens in a new window)
This solution uses the javascript library "FancyBox" and in my example I use the CDN version so you don't need to host anything on your server. Here is the javascript with some inline instructions in the comments:
// this loads the fancybox css and javascript from CDN - no need to change it
$('<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.min.css" type="text/css" media="screen" /><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.min.js"></script>').insertBefore('#knack-dist_1');
// this initializes fancybox and sizes the images
$(document).on(‘knack-scene-render.scene_1’, function(event, scene) {
// this finds each image on the scene and links them to the bigger version using fancybox
$("#kn-scene_1 img").each(function() {
// this part creates the URL to the full size image
// you must investigate your own setup to find the variable part of the URL
// for each of your images.
// in my example "thumb_2" is my thumbnail and "thumb_3" is the big version
// that is "fits within 1600x1600.
var thisimg = $(this).attr('src');
var fullimg = thisimg.replace('thumb_2','thumb_3');
// this takes the URL and puts a link around the photo thumbnail
$(this).wrap('<a class="fancybox" rel="group" href="'+fullimg+'">');
// this optionally makes the thumbnails responsive so that they always display
// nicely even on smaller screens
$(this).css('max-height', '200px');
$(this).css('max-width', '100%');
});
// initialize the javascript library
$(".fancybox").fancybox();
});
That is really all there is. The most awkward part is figuring out how to modify your image URL, but if you just right-click on any of the images and choose “Open Image in a New Window” you can then fuss with the “thumb_*” part of the URL until you find the version you want. I recommend the bigger size fits within 1000 or 1200px for the full screen version - it’s a decent compromise.
You will also have to replace “scene_1” with your scene’s ID.
Please let me know if you have any questions!
Benjamin
This thread is essentially deprecated since Knack has included features that achieve the same outcome as I hacked together here. Because of that, I am taking my example app offline as it counts against my app limit and I need it for something else :)
Onwards!
Benjamin
Hi Angel,
I have never included that part of FancyBox that shows a title or caption, but I assume that you could use the js variable "fullimg" in the code above to add it. You could take the last line (29 in my code above), and add similar parameters to the fancybox() call as is showin the JS Fiddle. While the title of the image is not something that I have available it seems like you could easily just set the caption to "Download this Image" with a link to "fullimg". Hopefully that helps. Good luck!
Benjamin
This is a fantastic post! Thank you very much for sharing. I have one quick question... I'm trying to add a download link to the title as demonstrated here: http://jsfiddle.net/zAe6Z/ but I'm not having any luck getting it to work. Any ideas on how to integrate this?
Cool - yeah, the code I put into the Knack admin is what is in the first post. It is not totally clear to me how Knack loads that code, but it works somehow :D Glad that you were able to use the tip/hack.
Ben, yeah the box works perfectly. I am just not seeing the js code anywhere. I have just been curious about it! (I like to question this...) Anyway thanks for taking time to answer.
Stevan, is the lightbox feature working when you click an image? If so, I am not sure where Knack puts the javascript that you set under "API and Code", but if the gallery is working then it's loading somewhere :) Maybe one of the Knack guys could chime in if your instance isn't working the same as mine.
You could also check in the network pane of the inspector to see if the cdnjs.cloudflare.com/ fancybox file is getting loaded. If it is, then it's somewhere even if the inspector can't see it.
Sorry - I don't feel like I am being very helpful, but I did confirm that my demo uses exactly the code from above. I'm happy to help out if there is any more info you can provide about what isn't working for you. Cheers,
Benjamin
Hi Ben. Actually I checked your demo. and only the css is showing up.
looking on the console, I cannot locate the js...
Hey Stevan,
#knack-dist_1 may only exist if you are looking at the site through the knack URL - I noticed some of the wrapper is different if you are embedding it someplace. If that isn't your problem, it's possible that the number would be different in your instance. You could try insertBefore('.kn-content') which may be more universal.
Basically, you want to get that call to the javascript for the lightbox plugin as high up on the page as you can so it loads before the content of the page. On my front-end, the knack-dist_1 is the first object inside the <body>.
Does that help?
Hi Ben. Nice.
Question about .insertBefore('Knack-dist_1'): I can see the css link but not the js.
Any explanantion?
Beautiful, thanks for sharing!