-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
165 lines (144 loc) · 4.35 KB
/
script.js
File metadata and controls
165 lines (144 loc) · 4.35 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
document.addEventListener('DOMContentLoaded', () => {
const lightbox = document.getElementById('lightbox');
const lbImage = lightbox.querySelector('img');
const closeBtn = document.getElementById('close');
// Create navigation arrows
const prevBtn = document.createElement('button');
prevBtn.innerHTML = '‹';
prevBtn.className = 'lb-nav lb-prev';
prevBtn.setAttribute('aria-label', 'Previous image');
const nextBtn = document.createElement('button');
nextBtn.innerHTML = '›';
nextBtn.className = 'lb-nav lb-next';
nextBtn.setAttribute('aria-label', 'Next image');
lightbox.appendChild(prevBtn);
lightbox.appendChild(nextBtn);
// Add styles for navigation arrows
const navStyle = document.createElement('style');
navStyle.textContent = `
.lb-nav {
position: absolute;
top: 50%;
transform: translateY(-50%);
background: rgba(0, 0, 0, 0.5);
color: white;
border: none;
font-size: 48px;
padding: 20px 15px;
cursor: pointer;
z-index: 1002;
transition: background 0.3s ease;
font-weight: 300;
line-height: 1;
}
.lb-nav:hover {
background: rgba(0, 0, 0, 0.8);
}
.lb-nav:disabled {
opacity: 0.3;
cursor: not-allowed;
}
.lb-prev {
left: 20px;
}
.lb-next {
right: 20px;
}
`;
document.head.appendChild(navStyle);
// Track current image and gallery
let currentImages = [];
let currentIndex = 0;
// Add animation styles
lightbox.style.transition = 'opacity 0.3s ease';
lbImage.style.transition = 'transform 0.3s ease';
// Open lightbox with animation
function openLightbox(fullSrc, allImages, index) {
currentImages = allImages;
currentIndex = index;
lbImage.src = fullSrc;
lightbox.style.display = 'flex';
lightbox.style.opacity = '0';
lbImage.style.transform = 'scale(0.9)';
updateNavButtons();
setTimeout(() => {
lightbox.style.opacity = '1';
lbImage.style.transform = 'scale(1)';
}, 10);
}
// Update navigation button states
function updateNavButtons() {
prevBtn.disabled = currentIndex === 0;
nextBtn.disabled = currentIndex === currentImages.length - 1;
}
// Navigate to previous image
function showPrevImage() {
if (currentIndex > 0) {
currentIndex--;
const fullSrc = currentImages[currentIndex].dataset.full || currentImages[currentIndex].src;
lbImage.style.transform = 'scale(0.95)';
setTimeout(() => {
lbImage.src = fullSrc;
lbImage.style.transform = 'scale(1)';
updateNavButtons();
}, 150);
}
}
// Navigate to next image
function showNextImage() {
if (currentIndex < currentImages.length - 1) {
currentIndex++;
const fullSrc = currentImages[currentIndex].dataset.full || currentImages[currentIndex].src;
lbImage.style.transform = 'scale(0.95)';
setTimeout(() => {
lbImage.src = fullSrc;
lbImage.style.transform = 'scale(1)';
updateNavButtons();
}, 150);
}
}
// Close lightbox with animation
function closeLightbox() {
lightbox.style.opacity = '0';
lbImage.style.transform = 'scale(0.9)';
setTimeout(() => {
lightbox.style.display = 'none';
}, 300);
}
// Gallery image clicks - use data-full attribute for full-size image
document.querySelectorAll('.gallery').forEach(gallery => {
const images = Array.from(gallery.querySelectorAll('img'));
images.forEach((img, index) => {
img.addEventListener('click', () => {
const fullSrc = img.dataset.full || img.src;
openLightbox(fullSrc, images, index);
});
});
});
// Navigation button events
prevBtn.addEventListener('click', (e) => {
e.stopPropagation();
showPrevImage();
});
nextBtn.addEventListener('click', (e) => {
e.stopPropagation();
showNextImage();
});
// Close events
closeBtn.addEventListener('click', closeLightbox);
lightbox.addEventListener('click', e => {
if (e.target === lightbox) closeLightbox();
});
// Keyboard navigation
document.addEventListener('keydown', e => {
if (lightbox.style.display === 'flex') {
if (e.key === 'Escape') {
closeLightbox();
} else if (e.key === 'ArrowLeft') {
showPrevImage();
} else if (e.key === 'ArrowRight') {
showNextImage();
}
}
});
});