I
I
ivasenkoartem2019-02-19 17:51:43
YouTube
ivasenkoartem, 2019-02-19 17:51:43

Playing YouTube videos while scrolling?

Why does not it work?

var player;

function onYouTubePlayerAPIReady() {
  player = new YT.Player('video');
}


var tag = document.createElement('script');
tag.src = "//www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);


var video_play = true;

var target = $('#video');
var targetPos = target.offset().top;
var winHeight = $(window).height();
var scrollToElem = targetPos - winHeight + 400;
$(window).scroll(function(){
  var winScrollTop = $(this).scrollTop();
  if(winScrollTop > scrollToElem){
  if (video_play == true) {
   		player.playVideo();
    	video_play = false;
  }
}
});

<iframe src="//www.youtube.com/embed/FKWwdQu6_ok?enablejsapi=1" width="700px" height="500px" frameborder="0" allowfullscreen id="video"></iframe>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question