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.

139 lines
4.1 KiB

10 months ago
<!DOCTYPE html>
<html>
10 months ago
10 months ago
<head>
<title>Image Gallery</title>
<style>
.gallery-img {
10 months ago
width: 100%;
10 months ago
height: auto;
object-fit: cover;
border-radius: 8px;
cursor: pointer;
}
10 months ago
10 months ago
#gallery {
display: grid;
10 months ago
grid-template-columns: repeat(10, 1fr);
/* 10 equal columns */
10 months ago
grid-gap: 10px;
padding: 10px;
margin: auto;
}
10 months ago
10 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;
10 months ago
z-index: 1000;
10 months ago
}
10 months ago
10 months ago
#lightbox img {
max-width: 80%;
max-height: 80%;
}
10 months ago
10 months ago
#lightbox #caption {
color: white;
margin-top: 20px;
10 months ago
position: absolute;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
text-align: center;
10 months ago
}
</style>
</head>
10 months ago
10 months ago
<body>
<header>
<h1>Image Gallery</h1>
</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>
10 months ago
var currentImageIndex = -1;
var imageArray = [];
10 months ago
function loadImages() {
var gallery = document.getElementById('gallery');
10 months ago
for (var i = 1; i <= 100; i++) { // Changed from i <= 100 to i < 100
10 months ago
let imageName = 'v' + i + '.png';
let img = document.createElement('img');
img.src = imageName;
10 months ago
img.onerror = function () { this.style.display = 'none'; };
img.onload = function () {
10 months ago
this.classList.add('gallery-img');
10 months ago
this.onclick = function () {
10 months ago
openLightbox(this.src, this.src.split('/').pop());
currentImageIndex = imageArray.indexOf(this);
};
// Add only visible images to the array
imageArray.push(this);
};
10 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';
}
10 months ago
// Keyboard navigation for lightbox
10 months ago
document.onkeydown = function (e) {
10 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
}
}
10 months ago
};
10 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);
}
}
10 months ago
window.onload = function () {
loadImages();
};
</script>
10 months ago
</body>
10 months ago
</html>