-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimages.js
61 lines (54 loc) · 2.33 KB
/
images.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Define a function to load images from a specified folder
function loadImagesFromFolder(folder, contentSelector) {
return new Promise((resolve, reject) => {
// Fetch image filenames from the specified folder
fetch(folder)
.then((response) => response.text())
.then((text) => {
// Extract image filenames from the HTML response
const filenames = text
.match(/href="([^"]+\.(?:png|jpg|jpeg|gif))"/gi)
.map((match) => match.split('"')[1]);
// Resolve with the array of image filenames
resolve(filenames);
})
.catch((error) => reject(error));
})
.then((filenames) => {
// Define content element after images are loaded
const contentElement = document.querySelector(contentSelector);
// Function to display images from the specified folder in random order for 8 seconds each
function displayImagesRandomly(images) {
// Function to display an image
function displayImage(imageSrc) {
contentElement.style.backgroundImage = `url(${imageSrc})`;
contentElement.style.backgroundSize = "cover"; // Cover the entire container
contentElement.style.backgroundPosition = "center"; // Center the image
}
// Function to select a random image
function selectRandomImage() {
const randomIndex = Math.floor(Math.random() * images.length);
return images[randomIndex];
}
// Function to display images in sequence with random timeout
function displayImageSequence() {
const imageSrc = selectRandomImage();
displayImage(imageSrc);
const timeoutDuration =
Math.floor(Math.random() * (10000 - 6000 + 1)) + 6000; // Random timeout between 7 and 11 seconds
setTimeout(displayImageSequence, timeoutDuration);
}
// Start displaying images in sequence
displayImageSequence();
}
// Display images randomly
displayImagesRandomly(filenames);
})
.catch((error) => {
console.error("Error loading images:", error);
});
}
// Load and display images for different folders and windows
loadImagesFromFolder("img/places/", ".places-content");
loadImagesFromFolder("img/people/", ".people-content");
// Add more calls to loadImagesFromFolder for additional folders and windows