Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
105 lines (86 loc) · 3.04 KB
/
Copy pathscript.js
File metadata and controls
105 lines (86 loc) · 3.04 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
96
97
98
99
100
101
102
103
104
105
document.addEventListener("DOMContentLoaded", () => {
// Filter functionality
const filterButtons = document.querySelectorAll(".filter-btn")
const galleryItems = document.querySelectorAll(".gallery-item")
filterButtons.forEach((button) => {
button.addEventListener("click", function () {
// Remove active class from all buttons
filterButtons.forEach((btn) => btn.classList.remove("active"))
// Add active class to clicked button
this.classList.add("active")
const filterValue = this.getAttribute("data-filter")
galleryItems.forEach((item) => {
if (filterValue === "all" || item.getAttribute("data-category") === filterValue) {
item.style.display = "block"
} else {
item.style.display = "none"
}
})
})
})
// Lightbox functionality
const lightbox = document.querySelector(".lightbox")
const lightboxImg = document.getElementById("lightbox-img")
const caption = document.querySelector(".caption")
const closeBtn = document.querySelector(".close-btn")
const prevBtn = document.querySelector(".prev-btn")
const nextBtn = document.querySelector(".next-btn")
let currentIndex = 0
let visibleItems = []
function updateVisibleItems() {
visibleItems = Array.from(galleryItems).filter((item) => item.style.display !== "none")
}
function openLightbox(index) {
updateVisibleItems()
currentIndex = index
const item = visibleItems[currentIndex]
const img = item.querySelector("img")
const text = item.querySelector(".overlay span").textContent
lightboxImg.src = img.src
caption.textContent = text
lightbox.style.display = "flex"
// Disable scrolling on body
document.body.style.overflow = "hidden"
}
function closeLightbox() {
lightbox.style.display = "none"
// Enable scrolling on body
document.body.style.overflow = "auto"
}
function navigateGallery(direction) {
currentIndex = (currentIndex + direction + visibleItems.length) % visibleItems.length
const item = visibleItems[currentIndex]
const img = item.querySelector("img")
const text = item.querySelector(".overlay span").textContent
lightboxImg.src = img.src
caption.textContent = text
}
// Open lightbox when clicking on gallery item
galleryItems.forEach((item, index) => {
item.addEventListener("click", () => {
openLightbox(index)
})
})
// Close lightbox
closeBtn.addEventListener("click", closeLightbox)
lightbox.addEventListener("click", (e) => {
if (e.target === lightbox) {
closeLightbox()
}
})
// Navigate through gallery
prevBtn.addEventListener("click", () => navigateGallery(-1))
nextBtn.addEventListener("click", () => navigateGallery(1))
// Keyboard navigation
document.addEventListener("keydown", (e) => {
if (lightbox.style.display === "flex") {
if (e.key === "Escape") {
closeLightbox()
} else if (e.key === "ArrowLeft") {
navigateGallery(-1)
} else if (e.key === "ArrowRight") {
navigateGallery(1)
}
}
})
})