Unbind/Destroy fancybox 2 events
Asked Answered
T

4

7

I am having a strange issue, I have code that pulls content via ajax and then binds Fancybox (2) to certain elements. The issue is that when I "refresh" this page I call the same function that pulls and binds Fancybox again.

My normal approach that has been working up until I changed to Fancybox 2 is to unbind the elements and rebind them. This however does not seem to be working with FB2.

I have tried the following:

$('.tool_button').unbind('click.fb');
$('.tool_button').unbind('click');
$('.tool_button').unbind();
$('.tool_button').off('click.fb');
$('.tool_button').off('click');
$('.tool_button').off();

I did notice the following in the source file which may be of use (starting line 652):

// Unbind the keyboard / clicking actions
    unbindEvents: function () {
        if (F.wrap && isQuery(F.wrap)) {
            F.wrap.unbind('.fb');
        }

        D.unbind('.fb');
        W.unbind('.fb');
    }

If anyone could help here it would be much appreciated.

Thanks in advance. Nick

Tiffanietiffanle answered 23/11, 2012 at 9:54 Comment(1)
I'm having the exact same problem. I created a jsFiddle to take things back to basics and try various methods. No luck so far jsfiddle.net/DigitalBiscuits/DBvt7/205Zonked
Z
13

If you include the attribute live:false then you will be able to remove the handlers from your elements using .off()

After digging through the source code i've also found that the specific event is click.fb-start, if you want to target it specifically.

For example:

$(".fancybox").attr('rel', 'gallery').fancybox({live: false});
//remove click handler
$('.fancybox').off("click.fb-start");

​ FYI In the documentation it says the following about live:

live: If set to true, fancyBox uses "live" to assing click event. Set to "false", if you need to apply only to current collection, e.g., if using something like - $( "#page" ).children().filter('a').eq(0).fancybox();

jsFiddle

Here is a jsFiddle demonstrating

http://jsfiddle.net/DigitalBiscuits/DBvt7/211/

Zonked answered 17/12, 2012 at 2:15 Comment(2)
You should flag this as the answer. I didn't catch this until I was going to answer about the fb-start event, after digging through source code as well.Volute
I had the same problem. Setting live: false worked for me without the need for unbinding the event.Vala
C
6

I know this is an old thread, but just wanted to confirm OAC Designs is absolutely right: Unbinding the .fancybox links only works if the live parameter is set to false.

However, you can also destroy Fancybox at the dom level, regardless of the live parameter, by doing:

$(document).unbind("click.fb-start");

Or

$(document).off("click.fb-start");


JSFiddle

Check out this JSFiddle for reference.

Chil answered 6/12, 2014 at 14:55 Comment(0)
R
0

You can use this code to destroy fancybox functionality on links:

jQuery('a').unbind('click.fb').removeData('fancybox');
Ragin answered 24/12, 2012 at 17:26 Comment(0)
T
0

Thanks for the response, I have not tested the above, I used a different approach to resolve the issue.

What I did was instead of using the default Fancybox functionality I created functions for where I would need Fancybox and manually called Fancybox in each case which thus didn't bind the event.

This isn't the best approach and the above may be better but I will get to testing this in the new year :)

Thanks again.

Tiffanietiffanle answered 27/12, 2012 at 10:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.