dimsemenov / PhotoSwipe

JavaScript image gallery for mobile and desktop, modular, framework independent
http://photoswipe.com
MIT License
24.18k stars 3.31k forks source link

Only first image is shown #1245

Open fibis opened 7 years ago

fibis commented 7 years ago

We use photoswipe on a website and have one problem: when we open a gallery, the slider counts all images, but only the first image will be shown, but it is possible to cycle thru all of the images. After closing and reopening the gallery, all images are shown. This is our js code:

function menuClicked(id) {
            var pswpElement = document.querySelectorAll('.pswp')[0];
            var items = [];
            $.getJSON("ajax/images?restaurant=" + id, function (data) {
                for (var i = 0; i < data.length; i++) {
                    var img = new Image();
                    img.src = data[i].path;
                    items.push(
                            {
                                src: data[i].path,
                                w: img.naturalWidth,
                                h: img.naturalHeight
                            }
                    );
                }
                var options = {

                    history: false,
                    focus: false,
                    closeOnScroll: false,

                    showAnimationDuration: 0,
                    hideAnimationDuration: 0

                };

                var gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
                gallery.init();
            });
        }
fbio commented 7 years ago

Got the same problem! Following this issue.

DerrickJames commented 7 years ago

I had this same issue, if you're generating the items list dynamically check if the dimensions of the images is correct. For my case, the images had 0px for both the width and height since I was fetching the dimensions from the Image() object and I was rendering only the first image so that a user can click to open the slide to view the rest of the images.

Hope this points someone towards the right direction.