放大弹出菜单-索引错误

Magnific Popup - Index error

本文关键字:索引 错误 菜单 放大      更新时间:2023-09-26

我无法让我的放大弹出菜单使用索引选项。我的目标是在点击图片的索引上打开图库。在我的HTML中,每个图像都有这样的内容:

<div class="open_gallery_index" data-id=5><img ... ></div>

然后我的JS看起来是这样的:

$(function(){
  $('.open_gallery_index').click(function(){
    var i = Number($(this).data("id"));
    $.magnificPopup.open({
      items: g_items,
      gallery: {
        enabled: true
      },
      type: 'image'
    },i);
  });
})

但当点击图像时,我得到一个错误

未捕获的类型错误:无法读取未定义的属性"已解析"

尽管如果我手动将变量I设置为5,一切都很好,所以问题一定在这个中

var i = Number($(this).data("id"));

但是我尝试了所有的方法都没有成功,谢谢你的帮助。

像这样更新data-id属性。

<div class="open_gallery_index" data-id="5"><img ... ></div>

我认为$.magnificPopup.open需要一个src属性。不太确定代码中的g_items是什么,但可以尝试以下操作:

$.magnificPopup.open({
  items: {
    src: '.open_gallery_index',
    type: 'image'
  }
  gallery: {
    enabled: true
  },
},i);
$.magnificPopup.open({
  items: g_items,
  gallery: {
    enabled: true
  },
  type: 'image'
},i);
i--;
i++;

它以这种方式对我起作用,我不知道为什么