Guest User

Failed lightbox

a guest
Oct 21st, 2025
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Title</title>
  7. <style>
  8. * {
  9. padding: 0;
  10. margin: 0;
  11. box-sizing: border-box;
  12. }
  13.  
  14. body {
  15. background: #ffffff;
  16. display: flex;
  17. align-items: center;
  18. justify-content: center;
  19. height: 100vh;
  20. color: #333;
  21. }
  22.  
  23. .gallery {
  24. display: grid;
  25. grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  26. gap: 15px;
  27. max-width: 800px;
  28. }
  29.  
  30. img {
  31. width: 100%;
  32. height: auto;
  33. }
  34.  
  35. .galleryItem {
  36. overflow: hidden;
  37. img {
  38. width: 100%;
  39. height: 100%;
  40. object-fit: cover;
  41. }
  42. }
  43.  
  44. #lightBox {
  45. display: none;
  46. align-items: center;
  47. justify-content: center;
  48. height: 100vh;
  49. position: fixed;
  50. left: 0;
  51. top: 0;
  52. background: rgba(0, 0, 0, 0.5);
  53. width: 100%;
  54. flex-direction: column;
  55.  
  56. img {
  57. width: 90%;
  58. max-width: 500px;
  59. height: 70%;
  60. max-height: 500px;
  61. object-fit: cover;
  62. }
  63.  
  64. #close {
  65. position: absolute;
  66. right: 30px;
  67. color: #ffffff;
  68. font-size: 50px;
  69. }
  70.  
  71. #caption {
  72. color: #000000;
  73. margin-top: 20px;
  74. font-size: 10px;
  75. text-align: center;
  76. }
  77. }
  78. </style>
  79. </head>
  80. <body>
  81. <!-- Galeria -->
  82. <div class="gallery">
  83. <div class="galleryItem">
  84. <img src="https://media.tenor.com/_zWYqfZdneIAAAAM/shocked-face-shocked-meme.gif" alt="Imagem 1" />
  85. </div>
  86.  
  87. <div class="galleryItem">
  88. <img src="https://www.bu.edu/files/2024/12/Screenshot-2024-12-13-at-12.42.40%E2%80%AFPM.png" alt="Imagem 2" />
  89. </div>
  90.  
  91. <div class="galleryItem">
  92. <img src="https://ichef.bbci.co.uk/images/ic/480xn/p0kbsfp5.jpg" alt="Imagem 3" />
  93. </div>
  94. </div>
  95. <!-- Lightbox -->
  96. <div id="lightBox">
  97. <span id="close">&times;</span>
  98. <img src="https://ichef.bbci.co.uk/images/ic/480xn/p0kbsfp5.jpg" alt="" class="lightBoxCon" />
  99. <div id="caption">Image</div>
  100. </div>
  101.  
  102. <script>
  103. const galleryItems = document.querySelectorAll(".galleryItem img");
  104. const lightbox = document.getElementById("lightBox");
  105. const lightboxImg = document.getElementById("lightBoxCon");
  106. const caption = document.getElementById("caption");
  107. const close = document.getElementById("close");
  108.  
  109. galleryItems.forEach((item) => {
  110. item.addEventListener("click", () => {
  111. lightbox.style.display = "flex";
  112. lightboxImg.src = item.src;
  113. caption.innerText = item.alt;
  114. });
  115.  
  116. close.addEventListener("click", (e) => {
  117. lightbox.style.display = "none";
  118. });
  119.  
  120. lightbox.addEventListener("click", (e) => {
  121. if (e.target !== lightboxImg && e.target !== caption) {
  122. lightbox.style.display = "none";
  123. }
  124. });
  125. });
  126. </script>
  127. </body>
  128. </html>
Advertisement
Add Comment
Please, Sign In to add comment