Using EasyFancybox Wordpress Plugin alongside Infi

2019-08-31 16:30发布

问题:

Trying to figure out how to reinitialize Easy FancyBox when new content is loaded in with infinite scroll. I tried $.fancybox.init(), but this doesn't seem to be enough. In the head section the script that calls easy fancybox is:

jQuery(document).ready(function($){
var fb_timeout = null;
var fb_opts = { 'overlayShow' : true, 'centerOnScroll' : true, 'showCloseButton' : true, 'showNavArrows' : true, 'onCleanup' : function() { if(fb_timeout) { window.clearTimeout(fb_timeout); fb_timeout = null; } } };
/* IMG */
var fb_IMG_select = 'a[href$=".jpg"]:not(.nofancybox),a[href$=".JPG"]:not(.nofancybox),a[href$=".gif"]:not(.nofancybox),a[href$=".GIF"]:not(.nofancybox),a[href$=".png"]:not(.nofancybox),a[href$=".PNG"]:not(.nofancybox)';
$(fb_IMG_select).addClass('fancybox').attr('rel', 'gallery');
$('a.fancybox, area.fancybox').fancybox( $.extend({}, fb_opts, { 'transitionIn' : 'elastic', 'easingIn' : 'easeOutBack', 'transitionOut' : 'elastic', 'easingOut' : 'easeInBack', 'opacity' : false, 'titleShow' : true, 'titlePosition' : 'over', 'titleFromAlt' : true }) );
/* Auto-click */ 
$('#fancybox-auto').trigger('click');
});
/* ]]> */

Any Ideas how I can reinitialize something like this, bound specifically to new content loaded into #content div.post? Thank you for any help.

回答1:

Check this thread, it may help (EasyFancybox uses fancybox v1.3.4)

Update: I just recalled that the thread of reference (above) will work for single new added elements, but not for galleries. If you have a set of galleries (using the rel attribute) then you may prefer to upgrade to jQuery 1.7+ (if not yet) and use jQuery on() instead of delegate(), that will allow you to initialize your existing and dynamically added elements.

I made an example page here that shows how to use jQuery on() to solve the issue of dynamically added elements and fancybox (v1.3.x), if you want to have a look.

Based on the example page, I guess in your specific case, you may try tweaking your code this way:

jQuery(document).ready(function($){
    var fb_timeout = null;
    var fb_opts = { 'overlayShow' : true, 'centerOnScroll' : true, 'showCloseButton' : true, 'showNavArrows' : true, 'onCleanup' : function() { if(fb_timeout) { window.clearTimeout(fb_timeout); fb_timeout = null; } } };
    /* IMG */
    $("#content div.post").on("focusin", function(){
        var fb_IMG_select = 'a[href$=".jpg"]:not(.nofancybox),a[href$=".JPG"]:not(.nofancybox),a[href$=".gif"]:not(.nofancybox),a[href$=".GIF"]:not(.nofancybox),a[href$=".png"]:not(.nofancybox),a[href$=".PNG"]:not(.nofancybox)';
        $(fb_IMG_select).addClass('fancybox').attr({'rel': 'gallery', 'tabindex': '1'});
        $('a.fancybox, area.fancybox').fancybox( $.extend({}, fb_opts, { 
            'transitionIn' : 'elastic', 
            'easingIn' : 'easeOutBack', 
            'transitionOut' : 'elastic', 
            'easingOut' : 'easeInBack', 
            'opacity' : false, 
            'titleShow' : true, 
            'titlePosition' : 'over', 
            'titleFromAlt' : true 
        }) );
        /* Auto-click */ 
        $('#fancybox-auto').trigger('click');
    }); // on
}); // ready

Of course, jQuery 1.7+ is required.