Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
84
rated 0 times [  87] [ 3]  / answers: 1 / hits: 115948  / 11 Years ago, thu, january 16, 2014, 12:00:00

I am interested in setting up an HTML page with multiple video clips such that each video clip plays only while visible and then pauses when out of view.



I have found this great example of how this can be implemented with one clip, but I have been unable to modify the code to work with multiple clips. Perhaps I need to convert this code into a function for easy re-usability?



Here is what I have so far (JS Bin linked above modified for 2 clips instead of one).



This code seems to work for only one of the two clips.



<!DOCTYPE html>
<html>
<!-- Created using jsbin.com Source can be edited via http://jsbin.com/ocupor/1/edit
-->
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<style>
#right {
position: absolute;
top: 2000px;
}
#video1 {
position: absolute;
left: 2000px;
top: 2000px;
}
#video2 {
position: absolute;
left: 2000px;
top: 3000px;
}

</style>

<style id=jsbin-css>
</style>
</head>
#
<body style=width: 4000px; height: 4000px;>
<div id=info></div>
<div id=down>
scroll down please...
</div>
<div id=right>
scroll right please...
</div>
<video id=video1>
<source src=http://video-js.zencoder.com/oceans-clip.mp4/>

</video>
<script>
var video = document.getElementById('video1'), fraction = 0.8;

function checkScroll() {
var x = video.offsetLeft, y = video.offsetTop, w = video.offsetWidth, h = video.offsetHeight, r = x + w, //right
b = y + h, //bottom
visibleX, visibleY, visible;

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) {
video.play();
} else {
video.pause();
}
}

checkScroll();
window.addEventListener('scroll', checkScroll, false);
window.addEventListener('resize', checkScroll, false);
</script>

<video id=video2>
<source src=http://video-js.zencoder.com/oceans-clip.mp4/>

</video>
<script>
var video = document.getElementById('video2'), fraction = 0.8;

function checkScroll() {
var x = video.offsetLeft, y = video.offsetTop, w = video.offsetWidth, h = video.offsetHeight, r = x + w, //right
b = y + h, //bottom
visibleX, visibleY, visible;

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) {
video.play();
} else {
video.pause();
}
} checkScroll();
window.addEventListener('scroll', checkScroll, false);
window.addEventListener('resize', checkScroll, false);

</script>

</body>
</html>

More From » html

 Answers
23

Using the isInViewport plugin and jQuery, here's my code for the task



$('video').each(function(){
if ($(this).is(:in-viewport)) {
$(this)[0].play();
} else {
$(this)[0].pause();
}
})

[#73128] Wednesday, January 15, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kurtisl

Total Points: 559
Total Questions: 110
Total Answers: 97

Location: Tokelau
Member since Sun, May 7, 2023
1 Year ago
kurtisl questions
;