javascript - how to stop youtube video playing when container closed -
i have seen few posts , answers find 1 video , not multiple videos on same page.
i have around 10 videos on web page. when click button each want overlay pop does. video played.
once user clicks exit button want video stop playing.
at minute if user no click pause of youtube video , exits out keeps playing in background.
i show 2 video sections example:
<div class="play-button"> <i class="fa fa-play"></i> </div> <div class="youtube-frame"> <div class="exit-youtube-frame"> <i class="fa fa-times"></i> </div> <iframe width="520" height="345" src="<?= $video ?>"></iframe> </div> <div class="play-button"> <i class="fa fa-play"></i> </div> <div class="youtube-frame"> <div class="exit-youtube-frame"> <i class="fa fa-times"></i> </div> <iframe width="520" height="345" src="<?= $video ?>"></iframe> </div>
now in jquery this:
jquery('.play-button').click(function(){ jquery('#overlay-youtube').show(); // overlay jquery(this).next().show(); }); jquery('.exit-youtube-frame').click(function(){ jquery(this).parents('.youtube-frame').hide(); jquery('#overlay-youtube').hide(); });
how incorporate method stop video playing each section?
i had same problem showing product videos on site , stopping them playing after closing overlay.
as long don't need pause video user can pick left off, can use detach()
method store video object in jquery re-add dom when user clicks show it.
here code 1 video (html jquery):
<div id="product-video-overlay"> <div id="video-container"> <a class="close-overlay">close</a> <iframe id="ytplayeroverlay" type="text/html" src="{youtubeurl}"></iframe> </div> </div> var videooverlay = $('#product-video-overlay'); var videoobject = videooverlay.find('#video-container'); var hasvideo = 1; $('li.video-thumb').click(function () { if (hasvideo == 0) { videoobject.appendto(videooverlay); } videooverlay.fadein(); }); $('#product-video-overlay, #product-video-overlay a.close-overlay').click(function (e) { e.stoppropagation(); videooverlay.fadeout(); videoobject.detach(); hasvideo = 0; });
since have multiple videos, need way identify play button corresponding overlay/video (maybe adding matching class each?).
as well, want store each detached element array retrieve later matching class or whatever method use link button proper video.
this how tackled problem. know there youtube api available use, did job.
good luck!
Comments
Post a Comment