add/remove images
i'm trying to add images to an instance of PhotoSwipe. any pointers?
i'm trying to make a JQuery gallery with many images that can't be loaded/shown together. i could attach the first 10images then load the next 10 and make a new instance of PhotoSwipe but i'm sure there is a way to add and remove images dynamically
i found a solution to add a new image to cache: (i copied the code from cache-initialize)
image={url: 'images/full/014.jpg', caption: 'Image 014'}
src = photoSwipeInstance.settings.getImageSource(image);
caption = photoSwipeInstance.settings.getImageCaption(image);
metaData = photoSwipeInstance.settings.getImageMetaData(image);
photoSwipeInstance.cache.images.push(new PhotoSwipe.Image.ImageClass(image, src, caption, metaData));
this only works if 'photoSwipeInstance' is started with at least 3images! (otherwise the dynamic creation of carousel items is not activated?)
in order to delete images i just shift()/pop() them:
photoSwipeInstance.cache.images.shift()
photoSwipeInstance.carousel.show(photoSwipeInstance.currentIndex-1)
that sounds like the solution for my problem: i have about 10 images but some of them are animated GIFs which should start when the user scrolls to them. do the images get removed when the user scrolls away and loaded when you scroll back?
and another (maybe stupid) question: where do i put the code you posted?
The API has changed a bit. You want to construct a new <img> fragment wrapped in an <a> tag and pass it in to the constructor.
@esib You want to execute the code whenever you want to add the new images. This could be anywhere you have a new collection of images to push (e.g. when you get an AJAX callback from your server with new image URLs).
Or short answer: you want to put this in the application Javascript, anytime after the gallery instance has been created.
I'm in need to add images to gallery dynamically, so how do i do this?
Do the cache method still work?
@andrewhao mention changes in API? What changes, and how do we use that?
Hi @tmikaeld,
You can do something similar to what I did for my project: https://github.com/porkbuns/shmile/blob/master/public/javascripts/shmile_gallery.js#L25
Hello Andrew,
Thanks for answering!
EDIT: I just got it to work, thanks! :-)
Question: How do i remove an image from the cache?
EDIT: I solved the cache too, just had to think a bit.
Hi,
Do you still need help? Seems like you've got it figured out (judging by your comment on the issue).
-Andrew
On Wed, Mar 14, 2012 at 12:32 PM, tmikaeld < [email protected]
wrote:
Hello Andrew,
Thanks for answering, i tried to use you'r example and when i click on my button, the html code is inserted but photoswipe won't load. I know photoswipe works, so it should be the event listener that's missing.
I'll just post the test-code here, it's easier.
photoViewer : function () { var opts = { autoStartSlideshow: false, slideshowDelay: 5000, cacheMode: Code.PhotoSwipe.Cache.Mode.normal, slideTimingFunction: "ease-in-out", loop: false }; gallery = $("#image-list a:first").photoSwipe(opts); // Add click event listener. $('.zoom').live('click', function (e) { e.preventDefault(); var imgTag = $(this).closest('.productItem').find('.large-image-holder img').attr('src'); altTag = $(this).closest('.productItem').find('.large-image-holder img').attr('alt'); debug.log(imgTag); var a = $('<a/>') .attr('href', imgTag) .append( $('<img />').attr('src', imgTag).attr('alt', altTag) ).appendTo('#image-list'); var src = gallery.settings.getImageSource(a[0]); var caption = gallery.settings.getImageCaption(a[0]); var metaData = gallery.settings.getImageMetaData(a[0]); gallery.cache.images.push(new Code.PhotoSwipe.Image.ImageClass(a, src, caption, metaData)) });
Reply to this email directly or view it on GitHub:
https://github.com/codecomputerlove/PhotoSwipe/issues/243#issuecomment-4506052
Hi,
Yes i solved it, just needed to read some source code to understand how it all works :-)
Thanks for your help Andrew!
And good luck with your project shmile, i have never tried node.js but i probably should.
when I try to append the new image to the list by $("#galleryOne").prepend("
"); $("ul.gallery a").photoSwipe(); it is ok and the image is shown and work but there is problem that this image isn't really added to the other images if I click on it,it works. but when click on its previous image and then click next it doesn't work So I tried this code $("#galleryOne").prepend("
"); var photoS = $("ul.gallery a").photoSwipe(); image={url: 'images/full/010.jpg', caption: 'Image 010'} src = photoS.settings.getImageSource(image); caption = photoS.settings.getImageCaption(image); metaData = photoS.settings.getImageMetaData(image); photoSwipe = Code.PhotoSwipe; photoS.cache.images.push(new photoSwipe.Image.ImageClass(image, src, caption, metaData)); as it is the same code that written in initialize method but the same problem happens and also when click on any image this error is shown: Uncaught Code.PhotoSwipe.activateInstance: Unable to active instance as another instance is already active for this target
Bump!
guys this is a really important issue! The capability of adding/removing, or doing any kind of manipulation on the gallery's contents as a response to user input would make it possible for other developers to integrate photoswipe not only in photo gallery apps, but on multiple other stuff!
I really did not find any documentation on this... and the methods provided in this thread are hacks and not official API..
Couldn't agree more with @simonfan, I've been trying for hours to get these hacks to work and they're just too buggy. Would be lovely to have a proper append/refresh method.
did you check http://stackoverflow.com/questions/10605720/adding-images-dynamically-to-photoswipe ? a friend just recommended this link, not sure if it'll work. would love your feedback, too
I wrote a function "addImage" for a PhotoSwipe instance. I know this pull request can't be merged the way it is right now, but I'm happy to get some comments on the code :)
Pull Request: https://github.com/codecomputerlove/PhotoSwipe/pull/611
Great @cynektix! :+1: