向下滚动时如何停止自动播放

时间:2016-04-20 17:15:42

标签: javascript html video

我在自动播放的标题中添加视频但是当用户向下滚动到页面时我无法停止视频..

2 个答案:

答案 0 :(得分:0)

试试这个。

您的浏览器不支持视频标记。我建议你升级你的浏览器。

答案 1 :(得分:0)

您必须为此目的使用Google youtube API。

 // 2. This code loads the IFrame Player API code asynchronously.
	  
	  // Youtuber 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);

      // 3. This function creates an <iframe> (and YouTube player)
      //    after the API code downloads.
      var player;
      function onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {
          height: '315',
          width: '560',
		  playerVars : {
                autoplay : 0,
                controls: 0,
                disablekb:0,
                modestbranding:1,
                showinfo:0

            },
          videoId: '3MGrgzTGdtE',
          events: {
			  
            'onReady': onPlayerReady,
            //'onStateChange': onPlayerStateChange
          }
        });
		player2 = new YT.Player('player2', {
          height: '315',
          width: '560',
		  playerVars : {
                autoplay : 0
            },
          videoId: '3MGrgzTGdtE',
          events: {
			  
            //'onReady': onPlayerReady2,
            //'onStateChange': onPlayerStateChange
          }
        });
      }

      // 4. The API will call this function when the video player is ready.
      function onPlayerReady(event) {
          event.target.mute();
        //event.target.playVideo();
      }
	   
      function stopVideo() {
        player.stopVideo();
      }
	  
	   $(window).scroll(function() {
		   var topOfOthnxtDiv = $("#parelaxhome").offset().top; 	
        $("#medicinediv").each( function() {
            if( ($(window).scrollTop() > $(this).offset().top)   ) {
               // $(this).css('opacity',1);
                player.playVideo();
            } if($(window).scrollTop() >= topOfOthnxtDiv)  {
                //$(this).css('opacity',0);
                player.stopVideo();
            }
        }); 
    });
	
	$(document).ready(function(e) {
        $('.pclose').click(function(){
			player2.stopVideo();
		})
		$('.clickpop').click(function(){
			player2.playVideo();
            player2.seekTo(0);
			player2.unMute();
		})
    });
<div class="videodiv">
	   <div id="myframe">
    <div id="play-video"></div>
    <div id="player"></div>
	</div>