简体   繁体   English

带有lightgallery插件的javascript中图像的动态路径

[英]Dynamic path for images in javascript with lightgallery plugin

I'm working on a project in craft cms and I want to make a gallery block that is added as an entry in craft.我正在处理工艺 cms 中的一个项目,我想制作一个画廊块,作为工艺条目添加。 After doing some research I was trying to do it with dynamic mode of lightgallery plugin.在做了一些研究之后,我试图用 Lightgallery 插件的动态模式来做。 The problem is that in dynamic mode, both sources and thumbnails for the gallery are defined in javascript, as an ex.问题在于,在动态模式下,图库的源和缩略图都在 javascript 中定义,例如。

$(document).ready(function () {
  $(".dynamic").on("click", function (e) {
    $(document).lightGallery({
      dynamic: true,
      dynamicEl: [
        {
          src: "",
          thumb: ""
        },
        {
          src: "",
          thumb: ""
        }
      ]

Until now when I was doing galleries with this plugin I was using html structure and for loops in twig like this :直到现在,当我用这个插件做画廊时,我在树枝中使用 html 结构和 for 循环,如下所示:

{% for image in entry.xxxx.all() %}
  <a href="{{image.url}}">
     <img src="{{image.url}}">
   </a>
{% endfor %}

Link to codepen for you to get some image of the idea https://codepen.io/Snopek/pen/zYwNxgL链接到 codepen 以获取该想法的一些图像https://codepen.io/Snopek/pen/zYwNxgL

Any ideas how to fit these two together?任何想法如何将这两者结合在一起?

Thx for help谢谢帮助

You will need to pass the list of images to JavaScript in order to access it in your lightbox initialization function.您需要将图像列表传递给 JavaScript,以便在灯箱初始化函数中访问它。 There are multiple ways to do that.有多种方法可以做到这一点。 For example, you could use the {% js %} tag to add some JavaScript code to your page that contains the encoded data.例如,您可以使用{% js %}标记向包含编码数据的页面添加一些 JavaScript 代码。 Or use the json_encode filter to add the data to your HTML element as a data attribute and use JSON.parse in your JavaScript code to parse the encoded JSON-string.或者使用json_encode过滤器将数据作为数据属性添加到 HTML 元素,并在 JavaScript 代码中使用JSON.parse来解析编码的 JSON 字符串。

Here's a goodarticle on passing data from Twig to JavaScript .这是一篇关于将数据从 Twig 传递到 JavaScript的好文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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