youtube "seek"
HTML
<p>If you hold the seek button at the same spot, you will see the overlay appear because youtube triggers a PAUSE event. How to make the overlay not show up?</p>
<div class="video">
<div id="ytvideo">
</div>
<div class="video-overlay">
<h1>OVERLAY</h1>
</div>
</div>
CSS
.video {
position: relative;
}
.video-overlay {
position: absolute;
top: 0;
left: 0;
display: none;
width: 400px;
height: 300px;
background: rgba(255, 0, 0, .7);
}
JavaScript
(function () {
var s = document.createElement("script");
s.src = "https://www.youtube.com/iframe_api";
var before = document.getElementsByTagName("script")[0];
before.parentNode.insertBefore(s, before);
})();
var ytvideo;
window.onYouTubePlayerAPIReady = function () {
ytvideo = new YT.Player('ytvideo', {
width: '400',
height: '300',
videoId: 'BOksW_NabEk',
playerVars: {
'autoplay': 1,
'controls': 1
},
events: {
"onStateChange": onPlayerStateChange
}
});
}
function showOverlay() {
if ($('.video .video-overlay').length) $('.video .video-overlay').fadeIn(1200);
}
function hideOverlay() {
if ($('.video .video-overlay').length) $('.video .video-overlay').hide();
}
var overlay;
function onPlayerStateChange(event) {
console.log(event.data);
clearTimeout(overlay);
// youtube fires 2xPAUSED, 1xPLAYING on seek
if (event.data == YT.PlayerState.PAUSED) {
overlay = setTimeout(showOverlay, 1000);
}
if (event.data == YT.PlayerState.PLAYING) {
overlay = hideOverlay();
}
}
$(document).ready(function () {
$('.video .video-overlay').on('click', function () {
ytvideo.playVideo();
});
});