youtube在页面锚点链接上嵌入自动播放点击

时间:2014-03-29 20:10:27

标签: javascript jquery youtube embed

我在页面底部嵌入了一个youtube剪辑,顶部有一个锚点链接,点击时滚动到它。

单击锚链接时是否可以自动播放嵌入的youtube剪辑?

我可以使用jQuery做一些关于锚链接的点击功能,自动播放youtube嵌入吗?

任何指针都非常感谢! 谢谢!!!

2 个答案:

答案 0 :(得分:1)

您需要使用youtube API执行此操作。这是一个演示者:http://plnkr.co/edit/lYCJeQBj0Cjl0FuzKTfE?p=preview

var player;
function onYouTubeIframeAPIReady() {
  //this is the id of your video
  player = new YT.Player('your-video');
}
//call this function from your links onclick
function playVideo() {
  if(player) {
    player.playVideo();
  }
}
//this loads the youtube api
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);


//iframe embed markup, note the enablejsapi=1 portion of the video URL
<iframe 
   id="your-video" 
   width="420" 
   height="315" 
   src="//www.youtube.com/embed/6yEgcb167k4?enablejsapi=1" 
   frameborder="0" 
   allowfullscreen
>
</iframe>

//your button markup
<a href="#your-video" onclick="playVideo()">Go to video</a>

答案 1 :(得分:0)

更轻松地播放YouTube视频 -

$("iframe")[0].contentWindow.postMessage("{\"event\":\"command\",\"func\":\"playVideo\",\"args\":\"\"}", "*");

确保iframe已加载$(window).load(function () {并将&amp;enablejsapi=1添加到网址。

相关问题