Search code examples
javascriptjqueryfancybox

How to get current image index in fancybox?


I am trying to get current image index in fancybox gallery. There is a pos method in documentation but it is just setter method and not working for getter.

$.fancybox.pos(3) // works fine
var index = $.fancybox.pos() // does not work . It returns undefined. 

I understand that pos is designed as setter method but i need to know current image index in fancybox gallery.
How can i know current image index ?


Solution

  • Something like the following should do just fine:

    var src = $('#fancybox-img').attr('src'); // Looks for the *fancyboxed* image
    var idx = $('a[href="'+src+'"]').index(); // Gets the index of the thumbnail
    

    You may have to adapt this solution if you use some custom DOM hierarchy.