lightbox.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. let currentLightboxImages = [];
  2. let currentLightboxIndex = 0;
  3. window.openLightbox = function(images, index) {
  4. currentLightboxImages = images;
  5. currentLightboxIndex = index;
  6. updateLightbox();
  7. document.getElementById('image-lightbox').classList.remove('hidden');
  8. };
  9. function updateLightbox() {
  10. if (!currentLightboxImages || currentLightboxImages.length === 0) return;
  11. const imgObj = currentLightboxImages[currentLightboxIndex];
  12. const imgEl = document.getElementById('lightbox-img');
  13. const capEl = document.getElementById('lightbox-caption');
  14. // Attempt local first, fallback to remote
  15. imgEl.src = imgObj.local;
  16. imgEl.onerror = function() {
  17. if (this.src !== imgObj.remote) {
  18. this.src = imgObj.remote;
  19. }
  20. };
  21. capEl.textContent = `${currentLightboxIndex + 1} / ${currentLightboxImages.length}`;
  22. }
  23. document.addEventListener('DOMContentLoaded', () => {
  24. const overlay = document.getElementById('image-lightbox');
  25. const btnClose = document.getElementById('lightbox-close');
  26. const btnPrev = document.getElementById('lightbox-prev');
  27. const btnNext = document.getElementById('lightbox-next');
  28. if (btnClose) {
  29. btnClose.addEventListener('click', (e) => {
  30. e.stopPropagation();
  31. overlay.classList.add('hidden');
  32. });
  33. }
  34. if (overlay) {
  35. overlay.addEventListener('click', (e) => {
  36. if (e.target === overlay) {
  37. overlay.classList.add('hidden');
  38. }
  39. });
  40. }
  41. if (btnPrev) {
  42. btnPrev.addEventListener('click', (e) => {
  43. e.stopPropagation();
  44. if (currentLightboxImages.length > 0) {
  45. currentLightboxIndex = (currentLightboxIndex - 1 + currentLightboxImages.length) % currentLightboxImages.length;
  46. updateLightbox();
  47. }
  48. });
  49. }
  50. if (btnNext) {
  51. btnNext.addEventListener('click', (e) => {
  52. e.stopPropagation();
  53. if (currentLightboxImages.length > 0) {
  54. currentLightboxIndex = (currentLightboxIndex + 1) % currentLightboxImages.length;
  55. updateLightbox();
  56. }
  57. });
  58. }
  59. // Keyboard navigation
  60. document.addEventListener('keydown', (e) => {
  61. if (!overlay || overlay.classList.contains('hidden')) return;
  62. if (e.key === 'Escape') {
  63. overlay.classList.add('hidden');
  64. } else if (e.key === 'ArrowLeft') {
  65. if (currentLightboxImages.length > 0) {
  66. currentLightboxIndex = (currentLightboxIndex - 1 + currentLightboxImages.length) % currentLightboxImages.length;
  67. updateLightbox();
  68. }
  69. } else if (e.key === 'ArrowRight') {
  70. if (currentLightboxImages.length > 0) {
  71. currentLightboxIndex = (currentLightboxIndex + 1) % currentLightboxImages.length;
  72. updateLightbox();
  73. }
  74. }
  75. });
  76. });