简体   繁体   中英

How to use PhotoSwipe for old versions of JavaScript by including it from a CDN?

PhotoSwipe only shows the code for ESM modules.

<script type="module">
import PhotoSwipeLightbox from 'photoswipe/dist/photoswipe-lightbox.esm.js';
const lightbox = new PhotoSwipeLightbox({
  gallery: '#my-gallery',
  children: 'a',
  pswpModule: () => import('photoswipe/dist/photoswipe.esm.js')
});
lightbox.init();
</script>

This does not work with my current setup. I'm working in Visual Studio on a project that uses jQuery and old ways of including JS files in HTML via <script> tags and CDNs.

If I use <script src='/path-to/photoswipe.esm.js'> it apparently won't work because this file contains an export keyword in it and it shows an error inside console:

Uncaught SyntaxError: Unexpected token 'export'

So, is there a way for me to use this library, but with old-school code?

v5 version offer umd build, you are able to use that build to run the old way

<!DOCTYPE html>
<html>
  <head>
    <title>Test Gallery</title>
  </head>
  <body>

    <script src="./photoswipe.umd.min.js"></script>
    <script src="./photoswipe-lightbox.umd.min.js"></script>
    
    <link rel="stylesheet" href="../photoswipe.css">

    <div class="test-gallery">
      <a href="https://dummyimage.com/1200x600/000/fff" data-pswp-width="1200" data-pswp-height="600">
        <img src="https://dummyimage.com/120x60/000/fff" alt="" />
      </a>
      <a href="https://dummyimage.com/1200x1200/000/fff" data-pswp-width="1200" data-pswp-height="1200">
        <img src="https://dummyimage.com/60x60/000/fff" alt="" />
      </a>
      <a href="https://dummyimage.com/600x1200/000/fff" data-pswp-width="600" data-pswp-height="1200">
        <img src="https://dummyimage.com/30x60/000/fff" alt="" />
      </a>
    </div>
    
    <script type="text/javascript">
      var lightbox = new PhotoSwipeLightbox({
        gallery: '.test-gallery',
        children: 'a',
        // dynamic import is not supported in UMD version
        pswpModule: PhotoSwipe 
      });
      lightbox.init();
    </script>

  </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM