2016-08-24 164 views
3

我正在使用Fancybox顯示單個HTML5視頻以及HTML5視頻庫的項目。在Fancybox中自動播放HTML5視頻

對於單個視頻,我打算在Fancybox中打開視頻後立即自動播放視頻,然後在視頻播放完畢後關閉Fancybox。

對於圖庫中的視頻,我想在Fancybox中打開視頻時自動播放視頻,當視頻播放完時前進到下一個圖庫項目,並暫停當前視頻/自動播放下一個/上一個視頻if用戶瀏覽畫廊。

我已經包括這裏codepen:http://codepen.io/ericjacksonwood/pen/kXKkaq/,其中包括一些我試圖用的fancybox的「負荷」功能的工作:

// Attempt 01  
$(".fancybox-video").find('video').play(); 

// Attempt 02 
$(".fancybox-video").find('video').attr('autoplay','autoplay'); 

// Attempt 03 
var vid = document.getElementsByClassName("fancybox-video"); 
function playVid() { 
    vid.play(); 
} 

任何幫助,將不勝感激!謝謝。

-------- --------編輯

的codepen已經更新,工作正常。

+0

你檢查的答案嗎? – Dekel

回答

7

您可以使用html5的媒體methodsevents

我使用play方法在幻燈片放映動畫完成後啓動視頻,當視頻結束時使用ended回調調用fancybox.next()函數。

這裏是一個工作版本,您可以檢查:

$(document).ready(function() { 
 
    $(".fancybox").fancybox({ 
 
    afterShow: function() { 
 
     // After the show-slide-animation has ended - play the vide in the current slide 
 
     this.content.find('video').trigger('play') 
 
     
 
     // Attach the ended callback to trigger the fancybox.next() once the video has ended. 
 
     this.content.find('video').on('ended', function() { 
 
     $.fancybox.next(); 
 
     }); 
 
    } 
 
    }); 
 
});
.fancybox-video { 
 
    display: none; 
 
} 
 

 
.fancybox-nav { 
 
    top: 50%; 
 
    -webkit-transform: translateY(-50%); 
 
      transform: translateY(-50%); 
 
    height: 40%; 
 
} 
 

 
/**** DEMO STUFF ****/ 
 
html, body { 
 
    padding: 1em; 
 
} 
 

 
hr { 
 
    border: none; 
 
    height: 1px; 
 
    background: #eee; 
 
    display: block; 
 
    clear: left; 
 
    margin: 4em 0 2em 0; 
 
} 
 

 
.video-links { 
 
    list-style: none; 
 
    padding: 0; 
 
} 
 
.video-links li { 
 
    float: left; 
 
    margin-right: 10px; 
 
}
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.min.css"> 
 
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.min.js"></script> 
 

 

 
<h4>01. Single Video</h4> 
 
<ul> 
 
    <li>Autoplay HTML5 video when Fancybox opens</li> 
 
    <li>Close Fancybox after video is finished playing</li> 
 
</ul> 
 

 
<ul class="video-links"> 
 
    <li><a class="fancybox" href="#single-video">Video 01</a></li> 
 
</ul> 
 

 
<div id="single-video" class="fancybox-video"> 
 
    <video controls width="100%" height="auto"> 
 
    <source src="https://cache.wdcdn.net/asset/assetId/2508128/size/primary/ts/1471978675/type/library/client/WD-8KVJQ/Oikos_TheSpill.mp4?token=fc1b0717c&category=pres&action=view" type="video/mp4"> 
 
    </video> 
 
</div> 
 

 
<hr/> 
 

 
<h4>02. Video Gallery</h4> 
 
<ul> 
 
    <li>Autoplay HTML5 video when Fancybox opens</li> 
 
    <li>Advance Fancybox to next video in gallery when video is finished playing</li> 
 
    <li>Pause video if user navigates to next video in gallery (which fancybox seems to do already)</li> 
 
    <li>Autplay next video if user navigates through the gallery</li> 
 
    <li>Close Fancybox after last video is finished playing</li> 
 
</ul> 
 

 
<ul class="video-links"> 
 
    <li><a class="fancybox" rel="video-gallery" href="#video01">Video 01</a></li> 
 
    <li><a class="fancybox" rel="video-gallery" href="#video02">Video 02</a></li> 
 
    <li><a class="fancybox" rel="video-gallery" href="#video03">Video 03</a></li> 
 
</ul> 
 

 
<div id="video01" class="fancybox-video"> 
 
    <video controls width="100%" height="auto"> 
 
    <source src="https://cache.wdcdn.net/asset/assetId/2508128/size/primary/ts/1471978675/type/library/client/WD-8KVJQ/Oikos_TheSpill.mp4?token=fc1b0717c&category=pres&action=view" type="video/mp4"> 
 
    </video> 
 
</div> 
 

 
<div id="video02" class="fancybox-video"> 
 
    <video controls width="100%" height="auto"> 
 
    <source src="https://cache.wdcdn.net/asset/assetId/2506071/size/primary/ts/1369681137/type/library/client/WD-8KVJQ/TaxAct_FreetoPee.mp4?token=fc1b0717c&category=pres&action=view" type="video/mp4">  
 
    </video> 
 
</div> 
 

 
<div id="video03" class="fancybox-video"> 
 
    <video controls width="100%" height="auto"> 
 
    <source src="https://cache.wdcdn.net/asset/assetId/2507394/size/primary/ts/1471978845/type/library/client/WD-8KVJQ/WalMart_Legs_15.mp4?token=fc1b0717c&category=pres&action=view" type="video/mp4"> 
 
    </video> 
 
</div>

+0

這是完美的。正是我在找什麼。如果您查看更新後的codepen,我還包含了第二個腳本,如果只有一個視頻,它將關閉fancybox。非常感謝你! –

+0

歡迎您:) – Dekel