這次做的網站,是一頁橫幅網站。裡面嵌入很多個youtube影片,然而要在影片上有一個圖片來擋著它們。除了擋住之外,還要在影片播完之後能夠自動往下一個影片的位置移動。(ps.但是移動這個,我就不說 XD)這次用的是youtube的iframeAPI來產生影片,參考網站寫法
<!DOCTYPE html>
<html>
<body>
<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="player"></div>
<script>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 6000);
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
</script>
</body>
</html>
<script>的語法是在頁面上先產生 <script src="https://www.youtube.com/iframe_api"></script>然後透過這api,它會自動呼叫 onYouTubeIframeAPIReady 這個function,然後就會產生一個影片....(對,只會產生一個)但是我們要的是產生多個影片啊!所以我多寫幾次就可以了....NO!((因為我試過 T^T))其實可以用以下方式修改,就是在寫一隻function,放在onYouTubeIframeAPIReady讓它去執行,就可以了。至於這隻fuction就看設計者的想法囉~=)ps.感覺真隨興阿
Continue reading