【发布时间】: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