//js
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function createVideo(vid){
return new YT.Player('top-player', {videoId: vid});
}
function onYouTubePlayerAPIReady() {
var video = $("#top-player").attr("data-id");
player = createVideo(video);
$('#play').on("click", function() {player.playVideo();});
$('#pause').on("click", function() {player.pauseVideo();});
}
//2. HTML markup. Add video id in 'data-id' attribute
/*
<div id="top-player" data-id="iIdvUYjnUCY"></div>
<a href="#" id="pause">Pause</a>
<a href="#" id="play">Play</a>
*/
Simple example how to play / pause youtube video
1 Response
Write a comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.