内嵌inline gallery不显示,已添加type="module"仍报import语法错误
问题修复方案
根因说明
你遇到的报错及gallery无法加载的问题由以下三个原因导致:
- 默认script标签不支持ES Module的
import语法,仅添加type="module"属性后还存在其他依赖问题 - 没有提前引入lightGallery核心库,仅导入了zoom、thumbnail两个插件,调用
lightGallery()方法时会找不到定义 - 第二张轮播图的thumb属性存在拼写错误,
lanscape.jpg应为landscape.jpg,会导致缩略图加载失败
修复后完整代码
<div class="d-flex justify-content-center"> <div id="inline-gallery-container" class="inline-gallery-container"></div> </div> <!-- 先引入第三方公共依赖 --> <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.min.js" integrity="sha384-Atwg2Pkwv9vp0ygtn1JAojH0nYbwNJLPhwyoVbhoPwBhjQPR5VtM2+xf0Uwh9KtT" crossorigin="anonymous"></script> <!-- 业务初始化脚本,添加type="module"支持import语法 --> <script type="module"> // 先导入lightGallery核心库 import lightGallery from "https://cdn.skypack.dev/lightgallery@2.0.0-beta.3"; // 再导入插件 import lgZoom from "https://cdn.skypack.dev/lightgallery@2.0.0-beta.3/plugins/zoom"; import lgThumbnail from "https://cdn.skypack.dev/lightgallery@2.0.0-beta.3/plugins/thumbnail"; const $lgContainer = document.getElementById("inline-gallery-container"); const inlineGallery = lightGallery($lgContainer, { container: $lgContainer, dynamic: true, hash: false, closable: false, showMaximizeIcon: true, appendSubHtmlTo: ".lg-item", slideDelay: 400, plugins: [lgZoom, lgThumbnail], dynamicEl: [ { src: 'hexagon.jpg', thumb: 'hexagon.jpg', subHtml: `<div class="lightGallery-captions"> <h4>Caption 1</h4> <p>Description of the slide 1</p> </div>`, }, { src: 'landscape.jpg', thumb: 'landscape.jpg', subHtml: `<div class="lightGallery-captions"> <h4>Caption 2</h4> <p>Description of the slide 2</p> </div>`, }, { src: 'hillsong-united-aftermath.jpg', thumb: 'hillsong-united-aftermath.jpg', subHtml: `<div class="lightGallery-captions"> <h4>Caption 3</h4> <p>Description of the slide 3</p> </div>`, }, ], thumbWidth: 60, thumbHeight: "40px", thumbMargin: 2 }); setTimeout(() => { inlineGallery.openGallery(); }, 200); </script>
内容的提问来源于stack exchange,提问作者A.Vind
相关产品推荐
相关产品推荐

