我有一個奇妙的Vimeo模式,經過一番努力,當模式通過'X'按鈕關閉時,我得到視頻停止。但是,由於我將關閉功能放在'X'按鈕上,如果用戶點擊遠離視頻關閉模式而不是使用按鈕,則視頻會繼續播放。當Bootstrap模式被解除時如何停止Vimeo視頻?
這裏就是我所說的stopVideo()
函數的onclick的HTML:
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close" onclick="stopVideo()">
<span aria-hidden="true">×</span>
</button>
</div>
這裏是停止視頻的JavaScript函數:
<script>
function stopVideo(){
var $frame = $('iframe#nofocusvideo');
// saves the current iframe source
var vidsrc = $frame.attr('src');
// sets the source to nothing, stopping the video
$frame.attr('src','');
// sets it back to the correct link so that it reloads immediately on the next window open
$frame.attr('src', vidsrc);
}
</script>
所以,我怎樣才能改變功能不應用於特定的關閉按鈕,而是應用於模式失去焦點的任何情況,例如點擊離開視頻?
我是一個新手,所以對我很容易。提前致謝!
編輯1:
我已經改變了腳本如下:
<script>
function stopVideo(){
var $frame = $('iframe#nofocusvideo');
// saves the current iframe source
var vidsrc = $frame.attr('src');
// sets the source to nothing, stopping the video
$frame.attr('src','');
// sets it back to the correct link so that it reloads immediately on the next window open
$frame.attr('src', vidsrc);
}
$('#promo-video').on('hidden.bs.modal', function (e) {
stopVideo();
})
</script>
的stopVideo()
功能不會被調用。任何想法我做錯了什麼?
編輯2: 下面是有問題的模式代碼:
<!-- VIDEO MODAL -->
<div class="modal fade" id="promo-video" tabindex="-1" role="dialog" aria-labelledby="modal-video-label">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close" onclick="stopVideo()">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="modal-video">
<div class="embed-responsive embed-responsive-16by9">
<iframe id="nofocusvideo" src="https://player.vimeo.com/video/180565514?api=1&player_id=vimeoplayer" name="vimeoplayer" width="640" height="360" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- End Video Modal -->
你應該閱讀你想隱藏的模態事件hidden.bs.mo DAL。 https://getbootstrap.com/javascript/#modals – Bosc
嘿@Bosc,我已經添加了一個編輯我的問題,你介意給它一看嗎?我似乎無法弄清楚我做錯了什麼。 –
剛剛經過測試,它適用於我,確保#promo-video是你給你的模式相同的ID – Bosc