Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close Lightbox Youtube video after has been playes with data-lity #206

Open
rodboc opened this issue Dec 13, 2018 · 8 comments
Open

Close Lightbox Youtube video after has been playes with data-lity #206

rodboc opened this issue Dec 13, 2018 · 8 comments

Comments

@rodboc
Copy link

rodboc commented Dec 13, 2018

Hi Guys,

I've used data-lity method to open the lightbox, like this

<a href="//www.youtube.com/watch?v=XSGBVzeBUbk" data-lity>iFrame Youtube</a>

But I haven't found the way to close the lightbox after the video has been played. Is there any way to get this done? I've seen another example related but only works when the instance is defined on JS.

Cheers guys

@rodboc
Copy link
Author

rodboc commented Dec 17, 2018

No one?

@ZIPITRO
Copy link

ZIPITRO commented Dec 17, 2018 via email

@trockendock
Copy link

I am facing almost same issue. I do want to close the lightbox from inside the iframe (meaning: target parent window to close the iframe).

I also read something about opening it programmatically, but not understanding it at all...

Thanks for any help

@trockendock
Copy link

just played around a bit:

opened iframe with

<script>
var myLity=null;
$('a').on('click',function(){
  myLity=lity('iframe.html');
  myLity.opener();
});
</script>

and then tried to close it from inside iframe with

<script>
$('button').on('click',function(){
    $(window.parent.document).myLity.close();
});
</script>

...unfortunately not working. somehow the parent window is not targeted or the opened instance "myLity" is not handed over...

@naton
Copy link

naton commented Feb 4, 2019

If I understand your question correctly, you’ll need to listen to the events emitted by the YouTube player within the iframe. Then you’ll be able to detect when the video has finished playing. See here for some examples: https://developers.google.com/youtube/iframe_api_reference#Getting_Started

@MPJHorner
Copy link

MPJHorner commented Oct 23, 2020

It's pretty simple, I've done it with Vimeo using like this (I assume YT has something similar).



  <script src="https://player.vimeo.com/api/player.js"></script>
    <script>
        $(document).on('lity:ready', function(event, instance) {
            console.log('Lightbox ready');
            var iframe = $('div.lity-container iframe')
            var player = new Vimeo.Player(iframe);

            player.on('ended', function() {
                console.log('Finished.');
                instance.close().then(function() {
                    console.log('Lightbox closed');
                });
            });
        });
    </script>

@Darkseal
Copy link

Darkseal commented Feb 8, 2021

Here's the Youtube version:

HTML

<link href="/lib/lity/3.0.0-dev/lity.min.css" rel="stylesheet">
<script src="/lib/lity/3.0.0-dev/lity.min.js"></script>
<script src="/lib/lity/3.0.0-dev/plugins/youtube/youtube.min.js"></script>

<a href="https://www.youtube.com/embed/DAIR374gDKA?enablejsapi=1" data-lity>Play</a>

JS

  <script type="text/javascript" src="https://www.youtube.com/iframe_api"></script>
  <script type="text/javascript">
      $(document).on('lity:ready', function (event, instance) {
          var iframe = $(".lity-iframe-container").find("iframe");
          iframe.prop("id", "lity-youtube-player");
  
          var player = new YT.Player("lity-youtube-player", {
              events: {
                  'onReady': function (e) {
                      e.target.playVideo(); // <- autoplay video
                  },
                  'onStateChange': function (e) {
                      if (e.data == YT.PlayerState.ENDED) {
                          instance.close(); // <- auto-closes lity after video ends
                      }
                  }
              }
          });
      });
  </script>

IMPORTANT NOTES

  • the enablejsapi=1 parameter is required.
  • Lity 3.0.0-dev or greater is required, as v2.4.1 (the currently "stable" one) has a weird QueryString handling feature that breaks the enablejsapi parameter while "forcing" the autoplay parameter, thus messing up everything. Luckily enough, v3.0.0-dev fixed such behavior.

For additional info & samples, read this post.

N.B.: If the author is up for it, I wouldn't mind add the Vimeo and Youtube APIs to the corresponding lity plugins to trasform these hacks into lity's legacy functions: drop a note if you're interested :)

@phenix-it
Copy link

Here's the Youtube version:

HTML

<link href="/lib/lity/3.0.0-dev/lity.min.css" rel="stylesheet">
<script src="/lib/lity/3.0.0-dev/lity.min.js"></script>
<script src="/lib/lity/3.0.0-dev/plugins/youtube/youtube.min.js"></script>

<a href="https://www.youtube.com/embed/DAIR374gDKA?enablejsapi=1" data-lity>Play</a>

JS

  <script type="text/javascript" src="https://www.youtube.com/iframe_api"></script>
  <script type="text/javascript">
      $(document).on('lity:ready', function (event, instance) {
          var iframe = $(".lity-iframe-container").find("iframe");
          iframe.prop("id", "lity-youtube-player");
  
          var player = new YT.Player("lity-youtube-player", {
              events: {
                  'onReady': function (e) {
                      e.target.playVideo(); // <- autoplay video
                  },
                  'onStateChange': function (e) {
                      if (e.data == YT.PlayerState.ENDED) {
                          instance.close(); // <- auto-closes lity after video ends
                      }
                  }
              }
          });
      });
  </script>

IMPORTANT NOTES

* the `enablejsapi=1` parameter is required.

* Lity 3.0.0-dev or greater is required, as v2.4.1 (the currently "stable" one) has a weird QueryString handling feature that breaks the `enablejsapi` parameter while "forcing" the `autoplay` parameter, thus messing up everything. Luckily enough, v3.0.0-dev fixed such behavior.

For additional info & samples, read this post.

N.B.: If the author is up for it, I wouldn't mind add the Vimeo and Youtube APIs to the corresponding lity plugins to trasform these hacks into lity's legacy functions: drop a note if you're interested :)

Hi @Darkseal
how to get your version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants