36
votes

On zurb foundation's website at http://foundation.zurb.com/docs/reveal.php they listed some Options including

open: callback function that triggers 'before' the modal opens.

opened: callback function that triggers 'after' the modal is opened.

close: callback function that triggers 'before' the modal prepares to close.

closed: callback function that triggers 'after' the modal is closed.

But I have no idea how to use them with my modal. I tried:

$('#myModal').closed(function()
{});

$('#myModal').trigger('reveal:closed')(
{});

$('#myModal').reveal.closed(function()
{});

$('#myModal').reveal().closed(function()
{});

I have Googled but found no hits. Anyone who can explain it or give me an example or provide a related link?

The suggestion given works, however I have yet another closely related question for reveal():

<a href="#" class="button" data-reveal-id="myModal2">Click Me For A Modal</a>);

I tried to add one attribute like data-closeOnBackgroundClick="false" That doesn't seem to work. What should be the correct syntax? Will it work for callback function as well?

9

9 Answers

15
votes

Call reveal like you normally would, but include the name of the option and corresponding function as an object:

//Reveal the modal and say "Good bye" when it closes
$("#myModal").reveal({ "closed": function () { alert("Good bye") } });
41
votes

The above answer did not work for me. Here's what worked (Foundation 4 and jQuery):

$('#myModal').bind('opened', function() {
  console.log("myModal opened");
});
21
votes

Event Bindings for Zurb Foundation Reveal -

There are a series of events that you can bind to for triggering callbacks:

$(document).on('open.fndtn.reveal', '[data-reveal]', function () {
  // your code goes here...
});

$(document).on('opened.fndtn.reveal', '[data-reveal]', function () {
  // your code goes here...
});

$(document).on('close.fndtn.reveal', '[data-reveal]', function () {
  // your code goes here...
});

$(document).on('closed.fndtn.reveal', '[data-reveal]', function () {
  // your code goes here...
});

If you have multiple data-reveal used in single page as follows :

<div class="content reveal-modal" id="element-1" data-reveal>
<div class="content reveal-modal" id="element-2" data-reveal>

Then in this situations you can trigger callback same as explained above but with little modification as shown below :

$(document).on('open.fndtn.reveal', '#element-1[data-reveal]', function () {
  // your code goes here...
});

$(document).on('open.fndtn.reveal', '#element-2[data-reveal]', function () {
  // your code goes here...
});
13
votes

On Zurb Foundation v6, these events were renamed to xxx.zf.reveal:

  • closeme.zf.reveal Fires immediately before the modal opens. Closes any other modals that are currently open
  • open.zf.reveal Fires when the modal has successfully opened.
  • closed.zf.reveal Fires when the modal is done closing.

Source: http://foundation.zurb.com/sites/docs/reveal.html#js-events

Examples:

  • Generic callback that will fire for all modals:

    $(document).on('open.zf.reveal', '[data-reveal]', function() {
      console.log('This works');
    });
    
  • Callback that will fire when a specific modal is opened:

    $(document).on('open.zf.reveal', '#<ELEMENT-ID>[data-reveal]', function() {
      console.log('This works');
    });
    
4
votes

Like meatrobot said, to get this working you want to bind to the modal with the action you are targetting. I got this to work:

$('#myModal').bind('closed', function() {
    console.log("myModal closed!");
});
2
votes

This is a little late but for the second part you add the attributes as a semi-colon separated list of values in the data-options attribute (tested with foundation 5), i.e:

<div id="myModal" data-options="close_on_background_click:false;" class="reveal-modal">

And no, you cannot pass functions this way, i tried :)

1
votes

Looking at Foundation 5 and found that the reveal library triggers open, opened, close, and closed events. Just attach a handler to the event you want.

$('#myModal').on([event], handler)

You can also pass the handlers via the settings argument. Check this out: https://github.com/zurb/foundation/blob/master/js/foundation/foundation.reveal.js#L92

1
votes

The foundation 5 documentation specifies scoping of reveal events to the 'reveal' eventspace. However, the actual modal events do not seem to fire consistently within this eventspace. Removing this specification fixes the issue:

$(document).on('opened.fndtn', '[data-reveal]', function() {
  console.log('This works');
});
0
votes

In foundation 3.2.5 you should bind 'reveal:opened' like this:

$('#myModal').bind('reveal:opened', function() {
   console.log("myModal opened");
});

Regards, MarianoC.