lightbox 2: how to add dynamically images via javascript

后端 未结 2 890
时光说笑
时光说笑 2021-01-23 02:11

I\'m having trouble attempting to dynamically add images, since lightbox2 (by lokesh dakar) is initialized after document load by existent html

相关标签:
2条回答
  • 2021-01-23 02:12

    To add existing img tags to be lightboxified on their own (without gallery functionality) one could use something like:

    $('img')
        .wrap(function(index) {
            return '<a href="'+$(this).attr('src')+'" data-lightbox="image-'+index+'"></a>';
        });
    

    Just add it BEFORE inserting the lightbox.js

    0 讨论(0)
  • 2021-01-23 02:19

    After a couple of hours spent on testing and reading the code, I came up with this magic code:

    //set album values
    IMG = $('<a href="...">')
        .attr('data-lightbox','roadtrip')
        .append('<img src="...">')
    //lightbox open:
    IMG.click(function(){
        lightbox.start($(this));
        return false;
    })
    

    some helpful tips:

    • don't set data with $(...).data('lightbox','group'), since lightbox uses the selector $('a[data-lightbox]') each time a link is clicked. Instead, you should use $(...).attr('lightbox','group').

    • lightbox.album is a temporary variable which you don't need to deal with.

    0 讨论(0)
提交回复
热议问题