简体   繁体   English

如何将 append 动态元素添加到在 lightgallery.js 中打开的图像

[英]How to append a dynamic element to an image opened in lightgallery.js

I have a page that show lots of photos which can be opened with lightgallery.js我有一个显示很多照片的页面,可以用 lightgallery.js 打开

I've already made some custom HTML caption which works fine.我已经制作了一些自定义的 HTML 标题,效果很好。 But now I want to add a label to the image itself.但是现在我想给图像本身添加一个 label。 How can I do that?我怎样才能做到这一点?

I will show an example of what my photos page looks like:我将展示我的照片页面的示例:

在此处输入图像描述

When you open an image this is what it looks like:当你打开一张图片时,它是这样的:

在此处输入图像描述

The left part is a custom HTML caption which I linked with this attribute: data-sub-html .左侧部分是自定义的 HTML 标题,我将其链接到此属性: data-sub-html

Example code of my looped code:我的循环代码的示例代码:

$photomasonry .= '
<div class="tile scale-anm noselect">
  <a href="https://studio.website.nl/'.$getphotos['preview'].'" data-sub-html="#caption'.$imgcount.'" class="item masonrytile" data-src="https://studio.website.nl/'.$getphotos['preview'].'">
    <img src="https://studio.website.nl/'.$getphotos['preview'].'"/>
    <span class="idlabel">'.$getphotos['id'].'</span>
    <span class="sizelabel">'.$size.'</span>
  </a>
</div>';
$photomasonry .= '
<div id="caption'.$imgcount.'" style="display:none">
  <img src="https://studio.website.nl/images/logo.png">
  <h4 class="subhtmlh4">Maak iets moois met deze foto</h4>
  <div class="gallerypopupbtns">
    <a href="design-magazijn/stockfotos/'.$_GET['alias'].'/'.$getphotos['id'].'"><button class="btnstyle purplebtn" type="button" name="button">Maak iets moois</button></a>
  </div>
</div>';
$imgcount++;

But now I want this purple size icon (L, XL, XX) as a label on the image when lightgallery is opened.但是现在我希望这个紫色大小的图标(L,XL,XX)在打开 lightgallery 时在图像上显示为 label。 How can I do this?我怎样才能做到这一点? The html of the lightgallery is only generated after the page is loaded. lightgallery的html是在页面加载后才生成的。

I want to add an element inside this part:我想在此部分中添加一个元素:

<div class="lg-img-wrap">
    <img class="lg-object lg-image" src="https://studio.website.nl/images/photos/previews/preview-bb58317c8d05e29b32963e7a295a5b9f.jpg">
</div>

But not just that, the content is dynamic, so display the correct size for the correct image.但不仅如此,内容是动态的,所以要为正确的图像显示正确的尺寸。 I already created this for the photo overview page but not when clicking a photo.我已经为照片概览页面创建了这个,但不是在单击照片时创建的。

According to the lightgallery docs , you can use the event lgAfterOpen to execute code once the gallery is opened.根据lightgallery 文档,您可以使用事件lgAfterOpen在画廊打开后执行代码。 However, lightgallery makes this slightly difficult as you can't access the image clicked on from this event, but you can access it from the event lgBeforeOpen .但是,lightgallery 使这有点困难,因为您无法访问从此事件单击的图像,但您可以从事件lgBeforeOpen访问它。 With this in mind, I'd accomplish this by doing two things.考虑到这一点,我会通过做两件事来实现这一点。

Firstly, add the size as a data attribute of each image:首先,将大小添加为每个图像的数据属性:

<img src="https://studio.website.nl/'.$getphotos['preview'].'" data-size="'.$size.'"/>

Secondly, add the following functions when initialising lightgallery:其次,在初始化lightgallery时添加如下函数:

// example gallery definition
const lg = document.getElementById('custom-events-demo');
// add the following functions
lg.addEventListener('lgBeforeOpen', (event) => {
  //add datasize property to target so it can be accessed in the afterOpen function
  event.target.dataset.size = event.explicitOriginalTarget.dataset.size;
});
lg.addEventListener('lgAfterOpen', (event) => {
  // get size from data attribute
  var size = event.target.dataset.size;
  // get the .lg-img-wrap div to append size to
  var imgWrap = document.querySelector(".lg-container.lg-show .lg-img-wrap");
  // create and append span
  var sizeSpan = document.createElement("span");
  sizeSpan.classList.add("sizelabel");
  sizeSpan.innerHTML = size;
  imgWrap.appendChild(sizeSpan);
});
//initialise lightgallery
lightGallery(lg);

You may be using a different version of lightgallery, but when I tested this the .lg-img-wrap element was a picture element not a div , so I replaced the last line of the event listener function with the following to get the size to show:您可能正在使用不同版本的 lightgallery,但是当我测试它时, .lg-img-wrap元素是一个picture元素而不是div ,所以我用以下代码替换了事件监听器 function 的最后一行以获得大小展示:

imgWrap.parentElement.appendChild(sizeSpan);

You might need a bit of extra CSS to position the size span as you wish, but just to get it to show all you need is:您可能需要一些额外的 CSS 到 position 大小跨度,但只是让它显示您需要的是:

.sizelabel {
  position: relative;
}

Note: I don't think this will work if you navigate to a different image from within lightgallery as it won't update the size, and in fact the size might just get removed.注意:如果您从 lightgallery 导航到不同的图像,我认为这不会起作用,因为它不会更新尺寸,事实上尺寸可能会被删除。 To fix this you'd probably need to do something using the lgAfterSlide event and the index of the image, but from your screenshot it looks like you don't expect the user to scroll through images within lightgallery anyway so this may not be an issue.要解决此问题,您可能需要使用lgAfterSlide事件和图像索引来做一些事情,但从您的屏幕截图来看,您似乎不希望用户无论如何都在 lightgallery 中滚动浏览图像,因此这可能不是问题.

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

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