0
votes

I've searched and searched but can only find very old posts about this sort of thing. What I am looking to do is create multiple Foundation 6 Reveal modals that open up YouTube videos that will autoplay, and stop video/audio when the modal is closed.

Here is a basic set up. Any help would be great!!

<p><a data-open="videoModal-01">Click me for a modal 01</a></p>
<p><a data-open="videoModal-02">Click me for a modal 02</a></p>

<!-- modal 01 -->
<div class="reveal" id="videoModal-01" data-reveal>
  <div class="responsive-embed">
    <iframe width="560" height="315" src="https://www.youtube.com/embed/4krs7z2bjlE" frameborder="0" allowfullscreen></iframe>
  </div>
  <button class="close-button" data-close aria-label="Close modal" type="button">
    <span aria-hidden="true">&times;</span>
  </button>
</div>

<!-- modal 2 -->
<div class="reveal" id="videoModal-02" data-reveal>
  <div class="responsive-embed">
    <iframe width="560" height="315" src="https://www.youtube.com/embed/c8aFcHFu8QM" frameborder="0" allowfullscreen></iframe>
  </div>
  <button class="close-button" data-close aria-label="Close modal" type="button">
    <span aria-hidden="true">&times;</span>
  </button>
</div>

Codepen

1

1 Answers

2
votes

I would create Reveal modals via JS and use Youtube IFrame Player API to create, load, start and stop video. Here is a partial prototype to get you started (it's missing player functionality).

var $dialog = $('#revealDialog');
if ($dialog.length === 0) {
    // init dialog
    $dialog = $('<div id="revealDialog" class="reveal dialog" data-reveal><div id="revealDialogContent"></div>');
    new Foundation.Reveal($dialog);
    // attach to to-be-loaded dialog close button(s) 
    $(document).on('click', '.button-dialog-close', function () {
        //stop player, e.g. player.stopVideo(); 
        $dialog.foundation('close');
        return false;
    });
}
// set dialog's content
$('#revealDialogContent').html('<div id="player"></div><button class="close-button" data-close aria-label="Close modal" type="button"><span aria-hidden="true">&times;</span>');

// create player and play it on the ready event
// player = new YT.Player('player'.....

// show it modal
$dialog.foundation('open');