javascript - Custom button for fancy box - Stack Overflow

I want to add a new button beside slide show, fullscreen, close, etc.and i need to attach a click even

I want to add a new button beside slide show, fullscreen, close, etc. and i need to attach a click event to that button so it gives me the src of the image that is currently showed.

I want to add a new button beside slide show, fullscreen, close, etc. and i need to attach a click event to that button so it gives me the src of the image that is currently showed.

Share Improve this question edited Mar 3, 2018 at 7:51 Pezhvak asked Mar 1, 2018 at 20:19 PezhvakPezhvak 10.9k7 gold badges31 silver badges44 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

well i googled a few times but didn't find a good solution, i ended up doing this and it works!

what i needed it for was to add a delete button to my fancybox.

to add a new button:

$.fancybox.defaults.btnTpl.delete = '<button data-fancybox-delete class="fancybox-button fancybox-button--delete" title="title of the icon">put your svg icon or whatever here..</button>';

to use the newly created button:

$.fancybox.defaults.buttons = [
        'slideShow',
        'fullScreen',
        'thumbs',
        'delete', // this one is the new button
        'close'];

and to attach click event and get the tag which triggered fancybox (i have data-id on that tag so i can send an xhr request to server to delete that photo)

$('body').on('click', '[data-fancybox-delete]', function(e) {
      var src = $('.fancybox-slide--current .fancybox-image').attr('src'); // src of the currently showing slide
      var idx = $('a[href="'+src+'"]')[0]; // My Tag

});

Another way to get src and element of the current photo (thanks to @Janis in the ments)

$('body').on('click', '[data-fancybox-delete]', function(e) {
      var src = $.fancybox.getInstance().current.src;
      var idx = $.fancybox.getInstance().current.opts.$orig;               
});

hope it helps somebody else as well.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744073810a4554025.html

相关推荐

  • javascript - Custom button for fancy box - Stack Overflow

    I want to add a new button beside slide show, fullscreen, close, etc.and i need to attach a click even

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信