【问题标题】:How to use PhotoSwipe for old versions of JavaScript by including it from a CDN?如何通过从 CDN 中包含旧版本的 JavaScript 来使用 PhotoSwipe?
【发布时间】:2022-09-28 00:04:50
【问题描述】:

PhotoSwipe 仅显示 ESM 模块的代码。

<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>

这不适用于我当前的设置。我在 Visual Studio 的一个项目中工作,该项目使用 jQuery 和通过 &lt;script&gt; 标签和 CDN 将 JS 文件包含在 HTML 中的旧方法。

如果我使用&lt;script src=\'/path-to/photoswipe.esm.js\'&gt;,它显然不起作用,因为该文件中包含export 关键字,并且在控制台中显示错误:

未捕获的语法错误:意外的令牌 \'export\'

那么,有没有办法让我使用这个库,但使用老式代码?

  • 你有什么解决办法吗?

标签: javascript


【解决方案1】:

v5 版本提供 umd 构建,您可以使用该构建以旧方式运行

<!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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    相关资源
    最近更新 更多