Is it possible to track how completely the user watched the video? (Reached the end) and only then open the blocks? Using jq
1 answer
You can, using the Iframe API and the onStateChange event. The onStateChange event returns 0 when playback is over. However, if you need the user to watch the entire video, then the rewind is not taken into account, that is, 0 will be returned when the video reaches the end, whether the user rewinds it or not.
More details about Iframe API and events can be read here:
API - https://developers.google.com/youtube/js_api_reference?hl=en
Developments -
https://developers.google.com/youtube/js_api_reference?hl=en#EventHandlers
// Загружаем Iframe 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); //Добавляем видео в элемент с ID player var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', { height: '390', width: '640', videoId: 'M7lc1UVf-VE', events: { 'onReady': onReady //Функция, которая вызовется когда видео загружено } }); } function onReady() { //Событие onStateChange вернет 0 когда воспроизведение окончено player.addEventListener('onStateChange', function(e) { console.log('State is:', e.data); if(e.data == 0){ //Выполняем нужные действия } }); } <div id="player"></div> |
var vid = document.getElementById("myVideo"); alert(vid.ended);HTML5 video. - Maxim Vlasov