I am having trouble making the mp4 video preloader responsive on my HTML page

Currently, I am facing two challenges while trying to set up an mp4 video in the pre-loader of my webpage. Firstly, the video is not responsive on my HTML page. Unlike its default behavior where it adjusts size according to screen variation, it fails to do so once loaded on the webpage. Secondly, the video does not trigger any header element to display after playing.

Expectations: I want the video to play first when the page loads and then show other content on the page. Additionally, I expect the video to behave as it would in its original form.

Below is the code snippet:

<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="styles.css">
  <title>Your Page Title</title>
</head>
<body>
  <!-- Preloader Container -->
  <div id="preloader-container">
    <video autoplay muted loop id="preloader-video">
      <source src="preloader.mp4" type="video/mp4">
      Your browser does not support the video tag.
    </video>
  </div>

  <header>Hello World</header>

  <script src="script.js"></script>
</body>
</html>

//styles.css

/* styles.css */
body {
  margin: 0;
  overflow: hidden; /* Prevent scrolling during preloading */
}

#preloader-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background-color: #fff; /* Background color for the preloader */
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999; /* Ensure the preloader is on top of other elements */
}

#preloader-container video {
  width: 100%; /* Make the video width cover the container */
  height: 100%; /* Make the video height cover the container */
  object-fit: cover; /* Maintain the video aspect ratio while covering the entire container */
  

}

// script.js

// script.js
document.addEventListener("DOMContentLoaded", function() {
  var preloaderVideo = document.getElementById("preloader-video");

  if (preloaderVideo) {
     // Reset video playback to the beginning when the page is loaded or refreshed
     preloaderVideo.currentTime = 0;
     
     // Hide the preloader container after the video has played
     preloaderVideo.addEventListener("ended", function() {
       document.getElementById("preloader-container").style.display = "none";
     });
   }
});

Answer №1

try replacing your code with the following:

// script.js
document.addEventListener("DOMContentLoaded", function() {
  var preloaderVideo = document.getElementById("preloader-video");

  if (preloaderVideo) {
    // Reset video playback to the beginning when the page is loaded or refreshed
    preloaderVideo.currentTime = 0;

    // Hide the preloader container after the video has played
    preloaderVideo.addEventListener("ended", function() {
      document.getElementById("preloader-container").style.display = "none";
    });
  }
});
/* styles.css */
body {
  margin: 0;
}

#preloader-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background-color: #fff; /* Background color for the preloader */
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999; /* Ensure the preloader is on top of other elements */
}

#preloader-container video {
  width: 100%;
  height: auto; /* Change height to auto to maintain the video's aspect ratio */
  max-height: 100%; /* Set max-height to 100% to prevent stretching on smaller screens */
  object-fit: cover; /* Maintain the video aspect ratio while covering the entire container */
}

header {
  /* Add styles for the header */
  text-align: center;
  padding: 20px;
  background-color: #333;
  color: #fff;
}
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8>
  <meta name="viewport" content="width=device-width, initial-scale=1.0"
  <link rel="stylesheet" href="styles.css">
  <title>Your Page Title</title>
</head>
<body>
  <!-- Preloader Container -->
  <div id="preloader-container">
    <video autoplay muted loop id="preloader-video">
      <source src="https://storage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" type="video/mp4">
      Your browser does not support the video tag.
    </video>
  </div>

  <header>Hello World</header>

  <script src="script.js"></script>
</body>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

eliminate currency match regular expression

Currently, I've implemented this regular expression to validate dollar amount inputs. parsley-regexp="^\$?[0-9][0-9\,]*(\.\d{1,2})?$|^\$?[\.]([\d][\d]?)$" I am now looking to modify the validation so that onl ...

The hyperlink refuses to open in a new tab

The jQuery BlockUI plugin is causing me some confusion. My goal is to include a clickable link within the content locked by the plugin, but no matter what I try - including adding target="_blank" - it just won't direct to the specified URL. Any sugg ...

What steps can I follow to ensure that the Twitter Bootstrap Collapsable Navbar is automatically open when the page

When the browser window is less than 980px wide and one visits Twitter Bootstrap's GitHub page, the navigation options are collapsed. To expand the options such as "Home", "Get Started", "Scaffolding," etc., simply click the button in the upper-right ...

Exploring the wonders of Vue.js and Laravel's type casting techniques

I have implemented DB2-style IDs for my database records in my Laravel 5.7 application, like this example: 201402241121000000000000. When trying to use it in my Vue component, I used the following syntax: <mycomponent v-bind:listing-key="{{ $listing-&g ...

Frustratingly Quiet S3 Upload Failures in Live Environment

Having trouble debugging a NextJS API that is functioning in development (via localhost) but encountering silent failures in production. The two console.log statements below are not producing any output, leading me to suspect that the textToSpeech call ma ...

Spinning cubemap texture in Three.js

I've created this cubetexture in Three.js. Now, I want to rotate the cubeTexture itself by 180 degrees, not the camera. Is there a way to achieve this? Specifically, I aim to rotate the x axis of the cubeTexture to display the opposite side. It would ...

How to customize the style of a td element in an HTML table

Attempting to modify the style of a td element: .valuator .sw-slots table, tr, td { width 100% } Even after trying to override with this code: td.license-name-td{ width: 100px !important; } The global style always takes precedence over my specific s ...

Preventing Bootstrap 4 slider images from shifting position when using background-attachment:fixed for a parallax effect

I'm trying to implement a Parallax scrolling effect on my Bootstrap 4 slider. However, every time the slider switches to the next image, the image needs to readjust itself into place. How can I avoid this? .customOverlayText { position: absolute; ...

Is it possible to implement an automatic scrolling functionality in this code snippet?

https://codepen.io/hexagoncircle/pen/jgGxKR?editors=0110 Stumbled upon some interesting code to tinker with and came across this piece. Attempted to search online for solutions, but my grasp on React (or maybe Java?) is close to zero. Tried following guid ...

The styling of Bootstrap collided with my CSS

While working on my web development project, I encountered an issue with the navigation bar. My friend was responsible for creating the nav bar, but when I integrated his code into my website, it clashed with Bootstrap and caused display errors. I tried va ...

The openlayers 3 function ol.proj.transform seems to be mysteriously turning live geolocation data into a string

I came across an interesting solution on stackoverflow for dynamically inserting data into a map using OpenLayers 3.*. Everything seems to be working fine when I directly input the numbers like this: geometry: new ol.geom.Point(ol.proj.transform([-72.07 ...

Tips for utilizing filters to search through various key values within an array of objects

In my collection of wines, each wine is represented by an object with specific data: var wines = [ { _id: '59a740b8aa06e549918b1fda', wineryName: 'Some Winery', wineName: 'Pinot Noir', wineColor: 'Red' ...

Unspecified OrbitControls Compatibility Issue between Angular2 and Three.js

I'm running into issues trying to set up OrbitControls in my Angular2 project. I managed to display a scene with a box, but I'm struggling to move the camera. Upon investigation, I found that my OrbitComponent, responsible for defining orbit con ...

New feature that allows color change to be black or white depending on background color

Can anyone suggest a function from material UI that can automatically change text color to either white or black based on the background color? For example: <AppBar color="primary"> <Toolbar> <Typography color="i ...

What is the best way to create a scrollable tbody in an HTML table using React?

In my current project, I am using React, Node, Express, and Postgres to fetch data from a database. The issue I'm facing involves creating a scrollable table within a div that spans the entire screen width. Initially, I had to apply display: block to ...

Ways to conceal just a portion of the bootstrap sidebar?

I'm looking to create a sidebar using 'vue-bootstrap' that can be hidden when clicked, but I only want to hide 80% of the width of the sidebar. How can I achieve this effect of partially hiding the sidebar? <b-button v-b-toggle.sidebar-1 ...

Implement a single CSS menu across various HTML pages

I'm currently working on developing a website. All of my pages have a common menu bar that I would like to include from a separate HTML file. Is there a way to include HTML files in order to address this concern? If so, what is the syntax for imple ...

A versatile tool to preview images prior to uploading

I have created a form with the ability to upload images in different columns. I would like to display a preview of these images before they are uploaded. I currently have a function that accomplishes this for a single upload: function readURL(input) { ...

The date format adjustments vary depending on the web browser being used

I have a JavaScript function that displays the date in the browser, but the format changes depending on the browser. For example, when I open my project in Chrome, the format is 4/30/2015, but when I open it in IE, it's displayed as 30 April, 2015. Ho ...

Enable the feature for users to upload images to a specific folder within the Chrome extension without the need for

I need to implement a feature in my Chrome extension that allows users to upload images directly to a specific folder named "upload" without needing a submit button. <form action="/upload"> <input type="file" name="myimages" accept="image/*"> ...