You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.2 KiB

11 months ago
<!DOCTYPE html>
<html>
11 months ago
11 months ago
<head>
<title>Image Gallery</title>
<style>
.gallery-img {
11 months ago
width: 100%;
11 months ago
height: auto;
object-fit: cover;
border-radius: 8px;
cursor: pointer;
}
11 months ago
11 months ago
#gallery {
display: grid;
11 months ago
grid-template-columns: repeat(10, 1fr);
/* 10 equal columns */
11 months ago
grid-gap: 10px;
padding: 10px;
margin: auto;
}
11 months ago
11 months ago
#lightbox {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
align-items: center;
justify-content: center;
11 months ago
z-index: 1000;
11 months ago
}
11 months ago
11 months ago
#lightbox img {
max-width: 80%;
max-height: 80%;
}
11 months ago
11 months ago
#lightbox #caption {
color: white;
margin-top: 20px;
11 months ago
position: absolute;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
text-align: center;
11 months ago
}
</style>
</head>
11 months ago
11 months ago
<body>
<header>
11 months ago
<!-- <h1>Image Gallery</h1> -->
11 months ago
</header>
<main>
<div id="gallery"></div>
</main>
<!-- Lightbox element -->
<div id="lightbox" onclick="closeLightbox()">
<img id="lightbox-img" src="#" alt="Preview">
<div id="caption"></div>
</div>
<script>
11 months ago
var currentImageIndex = -1;
var imageArray = [];
11 months ago
function loadImages() {
var gallery = document.getElementById('gallery');
11 months ago
for (var i = 0; i < 100; i++) { // Changed from i <= 100 to i < 100
11 months ago
let imageName;
10 months ago
if (i == -21) {
11 months ago
imageName = 'hsv.png';
} else {
imageName = 'v' + i + '.png';
}
11 months ago
let img = document.createElement('img');
img.src = imageName;
11 months ago
img.onerror = function () { this.style.display = 'none'; };
img.onload = function () {
11 months ago
this.classList.add('gallery-img');
11 months ago
this.onclick = function () {
11 months ago
openLightbox(this.src, this.src.split('/').pop());
currentImageIndex = imageArray.indexOf(this);
};
// Add only visible images to the array
imageArray.push(this);
};
11 months ago
gallery.appendChild(img);
}
}
function openLightbox(src, filename) {
document.getElementById('lightbox').style.display = 'flex';
document.getElementById('lightbox-img').src = src;
document.getElementById('caption').textContent = filename;
}
function closeLightbox() {
document.getElementById('lightbox').style.display = 'none';
}
11 months ago
// Keyboard navigation for lightbox
11 months ago
document.onkeydown = function (e) {
11 months ago
if (document.getElementById('lightbox').style.display === 'flex') {
if (e.key === 'ArrowRight') {
navigateLightbox(1); // Next image
} else if (e.key === 'ArrowLeft') {
navigateLightbox(-1); // Previous image
}
}
11 months ago
};
11 months ago
function navigateLightbox(direction) {
var newIndex = currentImageIndex + direction;
// Looping over if the index goes out of bounds
if (newIndex >= imageArray.length) {
newIndex = 0;
} else if (newIndex < 0) {
newIndex = imageArray.length - 1;
}
// Checking if the new image is visible
if (imageArray[newIndex].style.display !== 'none') {
currentImageIndex = newIndex;
var newImage = imageArray[currentImageIndex];
openLightbox(newImage.src, newImage.src.split('/').pop());
} else {
// Recursively call the function to skip non-visible images
navigateLightbox(direction);
}
}
11 months ago
window.onload = function () {
loadImages();
};
</script>
11 months ago
</body>
11 months ago
</html>