Stopping YouTube video when closing an overlay (modal/popup)

Sometime you want your videos to open in a nice overlay instead of a new window.
There are many solutions for that (modals in BootStrap, LightBox etc.), but since the video is always there – hidden or shown, it will still be heard when closing the overlay.  How can you control it or at least make it stop?

One solution would be to use YouTube’s API, but a much simpler solution would be to clear the “src” attribute for the iframe of the embedded video and adding it back when opening the overlay again.

Opening the video overlay:

Closing the video overlay:

 

Leave a Reply

Your email address will not be published. Required fields are marked *