-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
148 lines (123 loc) · 3.99 KB
/
script.js
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
// Select necessary elements
// const toggle = document.querySelector('.navbar-toggle');
// const menu = document.querySelector('.navbar-menu');
const navbarToggle = document.getElementById('navbar-toggle');
const navbarMenu = document.querySelector('.navbar-menu');
navbarToggle.addEventListener('click', () => {
navbarMenu.classList.toggle('active');
});
function toggleMenu() {
const menu = document.querySelector('.navbar-menu');
menu.classList.toggle('active');
}
const galleryItems = document.querySelectorAll('.gallery-item img');
const lightbox = document.createElement('div');
lightbox.className = 'lightbox';
document.body.appendChild(lightbox);
const lightboxContent = document.createElement('div');
lightboxContent.className = 'lightbox-content';
lightbox.appendChild(lightboxContent);
const lightboxImage = document.createElement('img');
lightboxImage.className = 'lightbox-image';
lightboxContent.appendChild(lightboxImage);
const caption = document.createElement('p');
caption.className = 'lightbox-caption';
lightboxContent.appendChild(caption);
const downloadBtn = document.createElement('a');
downloadBtn.className = 'action-btn';
downloadBtn.innerHTML = '📥 Download'; // Download icon
lightboxContent.appendChild(downloadBtn);
const shareBtn = document.createElement('button');
shareBtn.className = 'action-btn';
shareBtn.innerHTML = '📤 Share'; // Share icon
lightboxContent.appendChild(shareBtn);
const closeBtn = document.createElement('span');
closeBtn.className = 'close-btn';
closeBtn.innerHTML = '×'; // Close icon
lightboxContent.appendChild(closeBtn);
const prevBtn = document.createElement('button');
prevBtn.className = 'prev-btn';
prevBtn.innerHTML = '❮'; // Left arrow
lightboxContent.appendChild(prevBtn);
const nextBtn = document.createElement('button');
nextBtn.className = 'next-btn';
nextBtn.innerHTML = '❯'; // Right arrow
lightboxContent.appendChild(nextBtn);
toggle.addEventListener('click', () => {
menu.classList.toggle('active');
});
// Image data array
const imageData = [...galleryItems].map(item => ({
src: item.src,
alt: item.alt
}));
// Current index tracker
let currentIndex = 0;
// Open Lightbox
const openLightbox = index => {
currentIndex = index;
const { src, alt } = imageData[index];
lightboxImage.src = src;
caption.textContent = alt;
downloadBtn.href = src;
downloadBtn.download = `krishna-${index + 1}.jpg`;
lightbox.style.display = 'flex';
};
// Close Lightbox
const closeLightbox = () => {
lightbox.style.display = 'none';
};
// Navigate to Previous Image
const showPrevImage = () => {
currentIndex = (currentIndex - 1 + imageData.length) % imageData.length;
openLightbox(currentIndex);
};
// Navigate to Next Image
const showNextImage = () => {
currentIndex = (currentIndex + 1) % imageData.length;
openLightbox(currentIndex);
};
// Share Image
const shareImage = async () => {
const imageUrl = imageData[currentIndex].src;
if (navigator.share) {
try {
await navigator.share({
title: 'Krishna Gallery',
text: 'Check out this beautiful image of Krishna!',
url: imageUrl
});
} catch (error) {
console.error('Error sharing:', error);
}
} else {
alert('Share API not supported in this browser.');
}
};
// Event Listeners
galleryItems.forEach((item, index) => {
item.addEventListener('click', () => openLightbox(index));
});
closeBtn.addEventListener('click', closeLightbox);
prevBtn.addEventListener('click', showPrevImage);
nextBtn.addEventListener('click', showNextImage);
shareBtn.addEventListener('click', shareImage);
lightbox.addEventListener('click', event => {
if (event.target === lightbox) closeLightbox();
});
// Keyboard Navigation
document.addEventListener('keydown', event => {
if (lightbox.style.display === 'flex') {
switch (event.key) {
case 'ArrowLeft':
showPrevImage();
break;
case 'ArrowRight':
showNextImage();
break;
case 'Escape':
closeLightbox();
break;
}
}
});