cferdinandi / tabby

Lightweight, accessible vanilla JS toggle tabs.
MIT License
599 stars 73 forks source link

Stop video when tab is inactive #107

Closed bogdanptr closed 5 years ago

bogdanptr commented 5 years ago

Hello! I was wondering if there is a way to stop youtube embedded videos when a tab is inactive.

cferdinandi commented 5 years ago

You should be able to do this with the tabby event and the method described in this article.

Something like this.

var stopVideo = function ( element ) {
    var iframe = element.querySelector( 'iframe');
    var video = element.querySelector( 'video' );
    if ( iframe !== null ) {
        var iframeSrc = iframe.src;
        iframe.src = iframeSrc;
    }
    if ( video !== null ) {
        video.pause();
    }
};

document.addEventListener('tabby', function (event) {
    stopVideo(event.target);
}, false);