Tchacht Patrick
Tchacht Patrick

Reputation: 11

adding Picture dynamically to Photoswipe

I am trying to add pictures(from an array) dynamically to photoswipe. I have tried to use the "append" Method of jquery without a success.I also readed all about what people wrote about this issue on the web but i did not find the rigth answer.Any helps are welcome.

Upvotes: 1

Views: 670

Answers (1)

hgranlund
hgranlund

Reputation: 426

I found a way to add images dynamically to photoswipe, but it is a bit hackish.

There are 3 tings one must do:

  1. Update the originalImages (dont know how importent this is).
  2. Add click handlers to all images.
  3. Add all images to the instance.cache.images.

I did this with the following code:

        var images, image, i, metaData, src,caption,
        Util =window.Code.Util;
        images=$('#addedImages').find('a');
        instance.originalImages = $('#all-photo-swipe-images').find('.a');
        for (i = 0; i < images.length; i++) {
            image = images[i];
            src = instance.settings.getImageSource(image);
            caption = instance.settings.getImageCaption(image);
            metaData = instance.settings.getImageMetaData(image);
            image.__photoSwipeClickHandler = PhotoSwipe.onTriggerElementClick.bind(instance);
            Util.Events.remove(image, 'click', image.__photoSwipeClickHandler);
            Util.Events.add(image, 'click', image.__photoSwipeClickHandler);
            image = new PhotoSwipe.Image.ImageClass(image, src, caption, metaData);
            instance.cache.images.push(image);

Upvotes: 1

Related Questions