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.

140 lines
4.4 KiB

11 months ago
<!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-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;
11 months ago
z-index: 1000;
11 months ago
}
#lightbox img {
max-width: 80%;
max-height: 80%;
}
#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>
<body>
<header>
<h1>Image Gallery</h1>
</header>
<main>
11 months ago
<input type="range" id="layoutSlider" min="1" max="20" value="10" width="100%">
11 months ago
<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');
for (var i = 0; i <= 199; i++) {
let imageName = 'v' + i + '.png';
let img = document.createElement('img');
img.src = imageName;
11 months ago
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);
};
11 months ago
gallery.appendChild(img);
}
}
function updateGridLayout(columns) {
var gallery = document.getElementById('gallery');
gallery.style.gridTemplateColumns = `repeat(${columns}, 1fr)`;
}
document.getElementById('layoutSlider').addEventListener('input', function() {
updateGridLayout(this.value);
});
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
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
}
}
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);
}
}
window.onload = function() {
loadImages();
updateGridLayout(document.getElementById('layoutSlider').value);
};
</script>
11 months ago
</body>
</html>