How to get current image index in fancybox?

情到浓时终转凉″ 提交于 2020-02-04 01:07:39

问题


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 ?


回答1:


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.




回答2:


If you are using FancyBox 2 you can use the index property.

$.fancybox.current.index



回答3:


You can also write your own method to the Fancybox library (jscript_jquery.fancybox-1.3.4.pack.js):

b.fancybox.getPos=function(){return p};

Then you are able reach the index of actual image by call this:

var actualPosition = $.fancybox.getPos();



回答4:


i used accepted answer to upgrade for v3.2

var src = $('.fancybox-slide--current .fancybox-image').attr('src');
var idx = $('a[href="'+src+'"]')[0];

i needed to find currently showing slide, i needed it for my custom button (to delete that photo)




回答5:


Easiest and cleanest way to find the current index image in FancyBox 3 :

var idx = $.fancybox.getInstance().currIndex;

$('[data-fancybox="gallery"]').eq(idx).parents('.image_gallery').toggleClass('selected');



来源:https://stackoverflow.com/questions/7257394/how-to-get-current-image-index-in-fancybox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!