初始化图库时是否可以为每个图像设置字幕?

时间:2016-06-17 14:56:10

标签: javascript jquery magnific-popup

使用magnific popup,是否可以在通过javascript初始化图库时为每个图片设置字幕?每个图像都没有DOM元素,因此无法使用title属性(或任何其他属性)作为指向标题的指针。

$('#my-gallery').magnificPopup({
    items: [
        {
            src: 'image.jpg',
            title: 'Some Title'
        },
        {
            src: 'image2.jpg',
            title: 'Some Other Title'
        }
    ],
    gallery: {
        enabled: true
    },
    type: 'image'
});

1 个答案:

答案 0 :(得分:1)

您可以传递以下参数,根据大量弹出文档(http://dimsemenov.com/plugins/magnific-popup/documentation.html#image-type)为图片添加标题:

$('#my-gallery').magnificPopup({
    items: [
        {
            src: 'image.jpg',
            title: 'Some Title'
        },
        {
            src: 'image2.jpg',
            title: 'Some Other Title'
        }
    ],
    gallery: {
        enabled: true
    },
    type: 'image',
    image: {
       markup: '<div class="mfp-figure">'+
            '<div class="mfp-close"></div>'+
            '<div class="mfp-img"></div>'+
            '<div class="mfp-bottom-bar">'+
              '<div class="mfp-title"></div>'+
              '<div class="mfp-counter"></div>'+
            '</div>'+
          '</div>', // Popup HTML markup. `.mfp-img` div will be replaced with img tag, `.mfp-close` by close button

         tError: '<a href="%url%">The image</a> could not be loaded.' // Error message
   }
});

这是一个有效的jsfiddle: https://jsfiddle.net/wmboxLfr/3/

请注意,这里的关键是图像标记有&#39; mfp-title&#39;,并且项目将标题作为参数传递。干杯!