Here is a html and javascript code to manage Youtube and Vimeo videos to pause on scroll.
youtube 和 vimeo 的 iframe id 应该不同。它适用于同一页面上的多个视频。
<!DOCTYPE html>
<html>
<head>
<title>Pause Video on Scroll</title>
</head>
<body>
<iframe id="vdoplyr_9999" width="480" height="315" src="http://www.youtube.com/embed/K4pVQq0_4wg?enablejsapi=1&start=0&autoplay=0"
frameborder="0"></iframe>
<iframe id="vmoplyr_9999" width="480" height="275" src="https://player.vimeo.com/video/209375339" frameborder="0"></iframe>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
<div>Lorem ipsum text force scroll videos in iframe.</div>
</body>
<script src="https://www.youtube.com/iframe_api"></script> //youtube API
<script src="https://player.vimeo.com/api/player.js"></script> //vimeo API
<script>
var videos = document.getElementsByTagName("iframe"), fraction = 0.8;
function checkScroll() {
for(var i = 0; i < videos.length; i++) {
var video = videos[i];
var iframeid = document.getElementById(video.id);
if (video.id.indexOf('vdoplyr_') > -1) //youtube URL id
var playerytb = new YT.Player(video.id);
else if (video.id.indexOf('vmoplyr_') > -1) //vimeo URL id
var playervmo = new Vimeo.Player(iframeid);
var x = 0,
y = 0,
w = video.width,
h = video.height,
r, //right
b, //bottom
visibleX, visibleY, visible,
parent;
parent = video;
while (parent && parent !== document.body) {
x += parent.offsetLeft;
y += parent.offsetTop;
parent = parent.offsetParent;
}
r = x + parseInt(w);
b = y + parseInt(h);
visibleX = Math.max(0, Math.min(w, window.pageXOffset + window.innerWidth - x, r - window.pageXOffset));
visibleY = Math.max(0, Math.min(h, window.pageYOffset + window.innerHeight - y, b - window.pageYOffset));
visible = visibleX * visibleY / (w * h);
if (visible > fraction) {
if (video.id.indexOf('vdoplyr_') > -1) //if youtube video
iframeid.contentWindow.postMessage('{"event":"command","func":"' + 'pauseVideo' + '","args":""}', '*');
else if (video.id.indexOf('vmoplyr_') > -1) //if vimeo video
playervmo.pause();
}
}
};
window.addEventListener('scroll', checkScroll, false);
checkScroll();
</script>
</html>