首页 > 解决方案 > 从 lightGallery 插件获取 data-src

问题描述

我正在使用 lightgallery 插件http://sachinchoolur.github.io/lightGallery/docs/api.html#attributes

我想在单击图像时获取 data-src 值。

基本的HTML如下

<div id="work-grid" class="grid wow fadeIn animated">
   <div class="ecp-thumbnail element-item landing" data-iframe="true"  data- 
    src="image/landingpages/alta_1.jpg">
   <img class="img-responsive img-thumbnail" 
   src="image/landingpages/thumb_alta_1.jpg" />
  </div>
</div>

我的javascript如下

var $workGrid = $("#work-grid");

$workGrid.lightGallery({
    mode: 'lg-fade',
    cssEasing: 'cubic-bezier(0.25, 0, 0.25, 1)',
    download: false,
     share: false,
     selector: 'this'
});

$workGrid.on('onBeforeOpen.lg', function (event, prevIndex, index) {
    alert($workGrid.data('src'));
});

但我似乎无法获得点击图像的 data-src 属性值。

我添加了选择器:'this',但我无法弄清楚我应该如何使用它?

任何帮助,将不胜感激。

标签: javascriptlightgallery

解决方案


试试下面的代码

      var $lg =  $('#lightgallery').lightGallery({
            animateThumb: false,
            showThumbByDefault: false,
            controls:false
        });

        $lg.lightGallery();
        $lg.on('onBeforeSlide.lg',function(){
        let src =  $lg.data('lightGallery').$items.eq(0).data('src');
            getHighResolutionScreenShots(src);
        });

您也可以替换此功能 getHighResolutionScreenShots(src);


推荐阅读