Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
95 lines (78 loc) · 2.48 KB
/
Copy pathscript.js
File metadata and controls
95 lines (78 loc) · 2.48 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Simple JavaScript for beginners
let currentImageIndex = 0;
let allImages = [];
// Get all images when page loads
window.onload = function () {
allImages = document.querySelectorAll('.image-item img');
};
// Filter images by category
function filterImages(category) {
const items = document.querySelectorAll('.image-item');
const buttons = document.querySelectorAll('.filter-btn');
// Remove active class from all buttons
buttons.forEach(btn => btn.classList.remove('active'));
// Add active class to clicked button
event.target.classList.add('active');
// Show/hide images based on category
items.forEach(item => {
if (category === 'all' || item.dataset.category === category) {
item.classList.remove('hidden');
} else {
item.classList.add('hidden');
}
});
}
// Open lightbox
function openLightbox(clickedImg) {
const lightbox = document.getElementById('lightbox');
const lightboxImg = document.getElementById('lightbox-img');
// Find the index of clicked image
for (let i = 0; i < allImages.length; i++) {
if (allImages[i] === clickedImg) {
currentImageIndex = i;
break;
}
}
lightboxImg.src = clickedImg.src;
lightbox.style.display = 'block';
}
// Close lightbox
function closeLightbox() {
document.getElementById('lightbox').style.display = 'none';
}
// Change image in lightbox (next/previous)
function changeImage(direction) {
currentImageIndex += direction;
// Loop around if at beginning or end
if (currentImageIndex >= allImages.length) {
currentImageIndex = 0;
}
if (currentImageIndex < 0) {
currentImageIndex = allImages.length - 1;
}
document.getElementById('lightbox-img').src = allImages[currentImageIndex].src;
}
// Close lightbox when pressing Escape key
document.addEventListener('keydown', function (event) {
if (event.key === 'Escape') {
closeLightbox();
}
if (event.key === 'ArrowLeft') {
changeImage(-1);
}
if (event.key === 'ArrowRight') {
changeImage(1);
}
});
// Prevent lightbox from closing when clicking on image
document.getElementById('lightbox-img').onclick = function (event) {
event.stopPropagation();
};
document.querySelector('.prev').onclick = function (event) {
event.stopPropagation();
changeImage(-1);
};
document.querySelector('.next').onclick = function (event) {
event.stopPropagation();
changeImage(1);
};