colors/out/index.html
Michael Pilosov, PhD a02a662b6f image previews
2024-01-26 00:24:21 +00:00

144 lines
4.2 KiB
HTML

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