make background changing more instant

This commit is contained in:
SethBurkart123
2023-10-20 06:51:38 +11:00
parent b1c9beb3d9
commit 1edcf442e6
2 changed files with 29 additions and 23 deletions
-11
View File
@@ -17,17 +17,6 @@
height: 100%; height: 100%;
object-fit: cover; object-fit: cover;
animation: fade 0.4s ease-in-out;
}
@keyframes fade {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
} }
</style> </style>
</head> </head>
+29 -12
View File
@@ -32,7 +32,6 @@ const readData = async () => {
}); });
}; };
// Function to update the background
const updateBackground = async () => { const updateBackground = async () => {
try { try {
const data = await readData(); const data = await readData();
@@ -43,21 +42,39 @@ const updateBackground = async () => {
const url = URL.createObjectURL(data.blob); const url = URL.createObjectURL(data.blob);
const container = document.getElementById("media-container"); const container = document.getElementById("media-container");
container.innerHTML = ""; // Clear previous background
// Create new element and set properties
let newElement;
if (data.type === "image") { if (data.type === "image") {
const imgElement = document.createElement("img"); newElement = document.createElement("img");
imgElement.src = url; newElement.src = url;
imgElement.alt = "Uploaded content"; newElement.alt = "Uploaded content";
container.appendChild(imgElement);
} else if (data.type === "video") { } else if (data.type === "video") {
const videoElement = document.createElement("video"); newElement = document.createElement("video");
videoElement.src = url; newElement.src = url;
videoElement.autoplay = true; newElement.autoplay = true;
videoElement.loop = true; newElement.loop = true;
videoElement.muted = true; newElement.muted = true;
container.appendChild(videoElement);
} }
// Mark the old element for removal
const oldElement = container.querySelector(".current-media");
if (oldElement) {
oldElement.classList.remove("current-media");
oldElement.classList.add("old-media");
}
// Add the new element and mark it as current
newElement.classList.add("current-media");
container.appendChild(newElement);
// Delay removal of old element
setTimeout(() => {
const oldMedia = container.querySelector(".old-media");
if (oldMedia) {
oldMedia.remove();
}
}, 100); // 0.1 second delay
} catch (error) { } catch (error) {
console.error("An error occurred:", error); console.error("An error occurred:", error);
} }