gpt4 book ai didi

jquery - 使用Zurb Foundation Reveal.js启动/停止Youtube iFrame的最佳实践

转载 作者:行者123 更新时间:2023-12-03 05:54:38 26 4
gpt4 key购买 nike

这个问题已经被问过很多次了,但是很少被完整回答。由于这似乎是一个常见的UI问题,我想知道是否有人能以最佳方式完成模态下的Youtube视频/ Vimeo视频(在这种情况下,特别是与Reveal一起使用),该模式在打开时自动播放,在关闭时自动播放。

最佳答案

在花了几个小时找到解决方案时,这些解决方案在引入模板部件时会出现重大错误,而模板部件则在不同页面上重复了相同的模态,或者在引入多个模态时,我决定结合我所见的常见方法。现在可以使用,但是我想知道是否有更好的方法。

<html>

<a data-toggle="exampleModal8">

<div class="full reveal" id="exampleModal8" data-reveal>
<div id="player">

</div>

<button class="close-button" data-close aria-label="Close
reveal" type="button">
<span aria-hidden="true">&times;</span>
</button>
</div>
</html>

<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: 'YOURID GOES HERE; the numbers AFTER https://www.youtube.com/embed/',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}

// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.stopVideo();
// I changed the event.target above from startVideo to stopVideo. When the player is ready I don't want it to start until I open reveal. But not sure if this is the best way to do it?
}


// 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) {
// I DELETED THEIR CODE HERE IN THE SAMPLE TO STOP THE VIDEO AFTER 6 SECONDS AS SHOWN IN THE EXAMPLE
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
</script>
<script>
// This tells the Youtube Video to start and stop respectively when reveal opens or closes. Note that Foundation 6 has a new way to bind these events so make sure you're not using outdated JS from older version!
$(document).on('closed.zf.reveal', '[data-reveal]', function () {
player.stopVideo();
});
$(document).on('open.zf.reveal', '[data-reveal]', function () {
player.playVideo();
});
</script>

关于jquery - 使用Zurb Foundation Reveal.js启动/停止Youtube iFrame的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48393732/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com