Photoswipe 自动启动

Photoswipe automatically initiated

(我正在开发 symfony 2.3 以供参考)

加载我的页面时,photoswipe 会自动启动我的图像,而无需我自己激活。所以屏幕变暗,图片在中间弹出,我可以左右滑动但一开始看不到我的页面。图片只是在打开页面时隐藏所有内容。

我希望能够像 example

中那样通过触摸图像来激活它

我已经设置了photoswipe for my project and followed the doc accordingly regarding the implementation of the files

我以这种方式初始化了我的 JS(请注意,我没有在 JS 中添加我的 img,我使用 twig 循环添加它们,因为它们在我的数据库中)

var items = [];
var images = $('.fiche-vehicule-image img');

$.each(images, function () {
    items.push({
        src: $(this).attr('src'),
        w: 200,
        h: 200
    });
});
var options = {};
var pswpElement = document.querySelectorAll('.pswp')[0];
var gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
gallery.init();

这里是我的 html 我的幻灯片(与 bootstrap 一起使用)是:

<div id="myCarousel" class="carousel slide" data-ride="carousel">
    <div class="fiche-vehicule-image carousel-inner">
        {% for photo in vehicule.photos %}
            <div class="item {% if loop.first %}active{% endif %}">
              <img src="{{ photo.imgLarge }}" class="img-responsive">
            </div>
        {% endfor %}
    </div>
</div>

最后,为了让它工作,我在上面显示的轮播代码的 html.twig 中实现了 pswp 代码: 第 2 步:添加 PhotoSwipe (. pswp) 元素从 doc

到 DOM

好的,我终于找到了问题的答案。 我添加了这一行是为了让它工作。

$('.fiche-vehicule-image').on('click', function () {

所以完整的js就是这样

$('.fiche-vehicule-image').on('click', function () {
        var items = [];
        var images = $('.fiche-vehicule-image img');

        $.each(images, function () {
            items.push({
                src: $(this).attr('src'),
                w: 200,
                h: 200
            });
        });
        var options = {};
        var pswpElement = document.querySelectorAll('.pswp')[0];
        var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
        console.log(gallery);
        gallery.init();
    });