Issue
I am trying to set mp4 video in my pre-loader of my web page but I am facing two issues to achieve this target. [1:My video is not responsive and behave as its behave without html page, I mean by default video is responsive when I shrink or expand video size it occupy all space according to screen variation but when I load this video on my html page it is not responsive.
1:My video is not responsive and behave as its behave without html page, I mean by default video is responsive when I shrink or expand video size it occupy all space according to screen variation but when I load this video on my html page it is not responsive. 2:It does not run another header element or show header after playing the video. Expectations: I want to play the the video first when the page load then show other content on the page. Secondly video should behave as original. Here is my code //index.html
<!-- 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";
});
}
});
Solution
change you code with this :
// 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>
Answered By - naresh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.