这是indexloc提供的服务,不要输入任何密码
Skip to content

Possible to use photoswipe-dynamic-caption-plugin with photoswipe core? #27

@eshmh

Description

@eshmh

I am using Photoswipe without lightbox module (https://photoswipe.com/data-sources/#without-lightbox-module). The example provided here is based on lightbox

const captionPlugin = new PhotoSwipeDynamicCaption(lightbox, {
  // Plugins options, for example:
  type: 'auto',
});

How to load PhotoSwipeDynamicCaption with photoswipe? My current code looks like this:

<script type='module'>
        import PhotoSwipe from '../js/photoswipe.esm.min.js';
        import PhotoSwipeDynamicCaption from '../js/photoswipe-dynamic-caption-plugin.esm.min.js';
        document.querySelectorAll('td[id="img_link"] a').forEach((thumbnail, index) => {
            thumbnail.addEventListener('click', (e) => {
                e.preventDefault();

                const galleryElements = document.querySelectorAll('td[id="img_link"] a');
                const options = {
                    dataSource: [],
                    index: index, // Start at the clicked thumbnail
                    padding: {
                        top: 20,
                        bottom: 40,
                        left: 100,
                        right: 100
                    },
                    wheelToZoom: true
                };
                options.dataSource = Array.from(galleryElements).map(el => ({
                    src: el.getAttribute('href'),
                    width: parseInt(el.getAttribute('data-pswp-width')),
                    height: parseInt(el.getAttribute('data-pswp-height')),
                    alt: el.getAttribute('data-caption')
                }));

                const pswp = new PhotoSwipe(options);
                pswp.init();
            });
        });
    </script>

"alt" property in options.dataSource is the caption text.

Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions