【问题标题】:Iframe Video starting on scroll stuck on从滚动开始的 iframe 视频卡在
【发布时间】:2018-11-09 14:38:14
【问题描述】:

我正在使用 wordpress ACF 在此站点的 iframe 中嵌入视频:sofiajannok.com 视频在滚动到视频时开始,该功能本身起作用,并且在它正确播放之前,但是现在它陷入了“加载循环”。如果您按下 iframe 内的播放按钮,它会立即播放。有什么想法吗?

php(带有选项的 iframe)

<div class="slide-content cf">  
        <?php
        $iframe = get_field('slide_video_oembed', $page_ID);
        if($iframe): ?>

        <div class="vid-fullscreen">
            <?php   
                // get iframe HTML


                // use preg_match to find iframe src
                preg_match('/src="(.+?)"/', $iframe, $matches);
                $src = $matches[1];


                // add extra params to iframe src
                $params = array(
                    'controls'    => 1,
                    'hd'        => 1,
                    'rel'   => 0,
                    'autoplay' => 0,
                );


                $new_src = add_query_arg($params, $src);
                $enableJS = '&enablejsapi=1';
                $new_src .= $enableJS;
                $iframe = str_replace($src, $new_src, $iframe);


                // add extra attributes to iframe html
                $attributes = 'allowfullscreen';

                $iframe = str_replace('></iframe>', ' ' . $attributes . '></iframe>', $iframe);
                // echo $iframe
                echo $iframe;
                ?>
        </div>
        <? endif; ?>
</div>

JS-函数:

/********************************************************  
        //AUTOPLAY IFRAME
        - Autoplay video when iFrame is in view, pause when not
    ********************************************************/
        play_i = 0; 
      jQuery(window).scroll(function() {
        jQuery("iframe").each( function() {
            $this = jQuery(this);
            _src = $this.attr('src');
            _ID = $this.attr('id');
            _yPos = jQuery(window).scrollTop();
            _thisHT = $this.height();
            _thisTop = $this.offset().top;
            _thisBottom = _thisTop + _thisHT;



            if( _yPos > _thisTop*0.5 && _yPos < _thisBottom && play_i < 1) { //if iframe is in view
                $this[0].contentWindow.postMessage('{"event":"command","func":"playVideo","args":""}', '*'); //play video
                play_i++;
            } else {     
                $this[0].contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*');   //pause video
            } 
        }); 
    });

【问题讨论】:

    标签: javascript video iframe autoplay onscroll


    【解决方案1】:

    一段时间以来,移动设备上禁用了带声音的视频自动播放功能,最近该功能也开始在桌面浏览器上被禁用。网页不应尝试在没有用户交互(如“点击播放”)的情况下自动播放视频。

    唯一的解决方案是忘记视频自动播放并向用户显示“播放”按钮,以便他可以选择何时开始播放视频。

    【讨论】:

    • 所以自动播放甚至不适用于“静音”视频?另请注意,它仅在滚动到时启动,所以它应该像按钮一样工作,对吧?
    • @CarlPapworth 自动播放应该适用于静音视频,但并非总是如此(取决于浏览器和用户偏好)。
    猜你喜欢
    • 1970-01-01
    • 2021-09-17
    • 2018-03-16
    • 2013-10-19
    • 2022-01-14
    • 1970-01-01
    • 2019-07-23
    • 1970-01-01
    • 2014-08-06
    相关资源
    最近更新 更多