我正在努力实现
Instagram
,
Facebook
,
9GAG
以及ETC的视频自动播放系统,当窗口/屏幕滚动时,任何特定视频出现在窗口/屏幕的中间。所以我想创建一个系统,其中有许多
video
在页面上以及任何特定的
视频
出现在窗口/屏幕的中间,它应该设置
src
然后开始播放
视频
去除
SRC
以前的
视频
玩。我已经创建了自己的逻辑,但它只在滚动窗口时才起作用。
缓慢地
.如果快速滚动窗口,它将丢失当前视频播放的记录。所以我的逻辑不够好和有效。请帮我建立一个更好的游戏逻辑
视频
当它们出现在窗口中间时。
index.html:
window.onbeforeunload = function () {
this.scrollTo(0, 0);
}
var content = document.getElementById("content"),
video_player = undefined,
current = 0;
for (var x=0;x<50;x++) {
var video = document.createElement("video");
video.controls = false;
video.loop = true;
video.autoplay = true;
video.poster = "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c5/Big_buck_bunny_poster_big.jpg/220px-Big_buck_bunny_poster_big.jpg";
content.appendChild(video);
}
function scroll_function () {
if (this.oldScroll > this.scrollY) {
if (current >= 1) {
var previous_player = content.children[current-1];
if ((this.scrollY + this.innerHeight/2) < previous_player.offsetTop + previous_player.clientHeight) {
video_player.removeAttribute("src");
video_player.controls = false;
video_player.load();
current--;
video_player = content.children[current];
video_player.src = "https://dash.akamaized.net/akamai/bbb/bbb_640x360_60fps_1200k.mp4";
video_player.controls = true;
video_player.load();
}
}
} else {
if (current < 49) {
var next_player = content.children[current+1];
if ((this.scrollY + this.innerHeight/2) > next_player.offsetTop) {
video_player.removeAttribute("src");
video_player.controls = false;
video_player.load();
current++;
video_player = content.children[current];
video_player.src = "https://dash.akamaized.net/akamai/bbb/bbb_640x360_60fps_1200k.mp4";
video_player.controls = true;
video_player.load();
}
}
}
this.oldScroll = this.scrollY;
}
window.addEventListener("scroll", scroll_function);
video_player = content.children[current];
video_player.src = "https://dash.akamaized.net/akamai/bbb/bbb_640x360_60fps_1200k.mp4";
video_player.controls = true;
video_player.load();
body {
margin: 0;
}
#content {
height: 100%;
width: 75%;
margin-left: 12.5%;
}
video {
width: 100%;
height: 500px;
}
<html>
<head>
<title></title>
</head>
<body>
<div id="content"></div>
</body>
</html>