Twitter Bootstrap Modal stop Youtube video

后端 未结 21 1753
小蘑菇
小蘑菇 2020-11-27 11:57

I\'m very new to javascript and trying to use Twitter bootstrap to get a good looking website up and running quickly. I know this has something to do with jquery, but I\'m

21条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 12:05

    My solution to this that works for Bootstrap 3 and the modern YouTube embed format is as follows.

    Assuming your video is embedded within a standard Bootstrap 3 Modal with id="#video-modal", this simple bit of Javascript will do the job.

    $(document).ready(function(){
      $("#video-modal").on('hide.bs.modal', function(evt){
        var player = $(evt.target).find('iframe'),
            vidSrc = player.prop('src');
        player.prop('src', ''); // to force it to pause
        player.prop('src', vidSrc);
      });
    });
    

    I've seen proposed solutions to this issue involving use of the YouTube API, but if your site is not an https site, or your video is embedded using the modern format recommended by YouTube, or if you have the no-cookies option set, then those solutions don't work and you get the "TV set to a dead channel" effect instead of your video.

    I've tested the above on every browser I could lay my hands on and it works very reliably.

提交回复
热议问题