I have this Index. In this page I put a video background. The videos have a function to autoplay
and preload
. I want to add a preloader image that disappears after loading videos finishes.
<body onload="onload();">
<div class="fullscreen-bg">
<video preload="auto" id="idle_video" onended="onVideoEnded();" class="fullscreen-bg__video"></video>
<script>
var video_list = ["vid/video1.m4v",
"vid/video2.m4v",
"vid/video3.m4v",
"vid/video4.m4v"
];
var video_index = 0;
var video_player = null;
function onload() {
console.log("body loaded");
video_player = document.getElementById("idle_video");
video_player.setAttribute("src", video_list[video_index]);
video_player.play();
var vid = document.getElementById("idle_video");
vid.volume = 1;
}
function onVideoEnded() {
console.log("video ended");
if (video_index < video_list.length - 1) {
video_index++;
} else {
video_index = 0;
}
video_player.setAttribute("src", video_list[video_index]);
video_player.play();
}
</script>
</div>
Set the video element to "display:hidden" until the first video is loaded; then hide the image and set the video element to "display:block". Use the oncanplay or oncanplaythrough event to check when the first video is loaded. Something like this: (set the image source and class/style first)