【问题标题】:JQuery Autoplaying Youtube videos on scrollJQuery在滚动时自动播放Youtube视频
【发布时间】:2017-04-04 01:33:28
【问题描述】:

问题:我试图让 YouTube 视频在用户滚动到它时自动播放,并在用户滚动经过它时停止。

约束:JavaScript Web 开发的新手。

试过:离开https://jsfiddle.net/kmsdev/gsfkL6xL/ 我复制粘贴的一行一行只是为了让它工作。我熟悉 C++ 和 Java,所以我至少可以阅读代码的要点。看来我的代码是正确的,但我的 HTML5 中可能没有正确调用它?

英雄部分:

<section class="video_background">
    <div class="video_foreground">
        <iframe id="playerA" frameborder="0" title="YouTube video player" type="text/html" src="https://www.youtube.com/embed/u1zgFlCw8Aw?enablejsapi=1"></iframe>
    </div>
</section>

JS 部分:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript" src="assets/js/scroll.js"></script>

JS:

$(function() {
  $('a[href*="#"]:not([href="#"])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        $('html, body').animate({
          scrollTop: target.offset().top
        }, 1000);
        return false;
      }
    }
  });
});

我在https://jsfiddle.net/kmsdev/gsfkL6xL/ 上面有这个,但这不重要。

【问题讨论】:

    标签: javascript jquery html youtube


    【解决方案1】:

    这是一个工作示例,其中包含所有 css/js。我看不到你对 PlayYTVideos 源版本做了什么,但你可能忘记了 window.onload。

    <!DOCTYPE html>
    <html>
    <head>
        <script type="text/javascript" src="//code.jquery.com/jquery-2.1.4.js"></script>
        <link rel="stylesheet" type="text/css" href="/css/result-light.css">
        <script type="text/javascript" src="https://www.youtube.com/iframe_api"></script>
        <style type="text/css">
            iframe {
                width: 200px;
                height: 200px;
                margin: 500px 0;
            }
        </style>
        
        <script type='text/javascript'>//<![CDATA[
        window.onload=function(){
            var LoadVideo = function(player_id){
                var Program = {
    
                    Init: function(){
                        this.NewPlayer();
                        this.EventHandler();
                    },
    
                    NewPlayer: function(){
                        var _this = this;
                        this.Player = new YT.Player(player_id, {});
                        _this.Player.$element = $('#' + player_id);
                    },
    
                    Play: function(){
                        if( this.Player.getPlayerState() === 1 ) return;
                        this.Player.playVideo();
                    },
    
                    Pause: function(){
                        if( this.Player.getPlayerState() === 2 ) return;
                        this.Player.pauseVideo();
                    },
    
                    ScrollControl: function(){
                        if( Utils.IsElementInViewport(this.Player.$element[0]) ) this.Play();
                        else this.Pause();
                    },
    
                    EventHandler: function(){
                        var _this = this;
                        $(window).on('scroll', function(){
                            _this.ScrollControl();
                        });
                    }
                };
                var Utils = {
                    /** @author http://stackoverflow.com/a/7557433/1684970 */
                    IsElementInViewport: function(el){
                        if (typeof jQuery === "function" && el instanceof jQuery) el = el[0];
                        var rect = el.getBoundingClientRect();
                        return (
                                rect.top >= 0 &&
                                rect.left >= 0 &&
                                rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
                                rect.right <= (window.innerWidth || document.documentElement.clientWidth)
                        );
                    }
                };
                return Program.Init();
            };
            LoadVideo('playerA');
            LoadVideo('playerB');
            LoadVideo('playerC');
        }//]]>
    
        </script>
        
    </head>
    <body>
    <iframe id="playerA" frameborder="0" title="YouTube video player" type="text/html" src="https://www.youtube.com/embed/u1zgFlCw8Aw?enablejsapi=1"></iframe>
    <iframe id="playerB" frameborder="0" title="YouTube video player" type="text/html" src="https://www.youtube.com/embed/u1zgFlCw8Aw?enablejsapi=1"></iframe>
    <iframe id="playerC" frameborder="0" title="YouTube video player" type="text/html" src="https://www.youtube.com/embed/u1zgFlCw8Aw?enablejsapi=1"></iframe>
    </body>
    </html>

    【讨论】:

    • 哇,谢谢!我忘记了 window.onload 并且没有获取我的 youtube javascript api
    猜你喜欢
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    • 2023-03-05
    相关资源
    最近更新 更多