Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How to play and pause video in lightbox with jQuery

Does anybody knows how to play and pause an video which is displayed in a lightbox?

The Plan is the following:

  1. I want to display a youtube-video within a lightbox using jQuery. I figured out the code and it's working.

  2. If the user clicks on a close-button, I want to hide the overlay and the video to stop playing. I didn't figured out this last part yet.

Anybody can help me? Maybe there's a course which covers this up?

Here's my code-snippet – html and jQuery for the btn-close-function :

<div class="vt-wrap vt-claudia-wrap">
    <p class="btn btn-close"><i class="fa fa-times"></i></p>
    <div class="vt-video">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/m4-uEa7tr4A?rel=0&amp;showinfo=0" frameborder="0" allowfullscreen></iframe>
        <p><strong>Name of Person</strong></p>
    </div>
</div>
var $videoClaudia = jQuery(".vt-claudia-wrap");
$overlay.append($videoClaudia);

jQuery(".vt-wrap").hide();

//... some other code for $overlay...

// Here's my Problem: 
jQuery(".btn-close").click(function () {
    $overlay.fadeOut();
    jQuery(".vt-video").get(0).pause();
});

Can I actual solve this problem by using jQuery? Glad if anybody could help me out with this...

You can also look at it live on www.neu.afoma.net – just scroll down to the blue section where it says "Meinungen zu unseren Online Kursen" (engl. = testimonials) and click the green button "Video ansehen" (engl. = play video).

It should work like the video here on this side in the right sidebar "Tips for asking questions".

Thanks for your help,

Anke

Thanks for this link Andrew, I will definitely look into this! If I'm going to have a lot more control I don't mind writing more code ;-)

Again, Thanks!

1 Answer

You probably want to be using the API for this: https://developers.google.com/youtube/iframe_api_reference

Check that out, it might be a bit more work embedding the video in the lightbox but you'll have a lot more control over the video itself.