Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [JavaScript][CSS] Otwieranie zdjęcia w osobnym oknie
david8213
post
Post #1





Grupa: Zarejestrowani
Postów: 308
Pomógł: 0
Dołączył: 9.12.2009

Ostrzeżenie: (10%)
X----


Czy da się uprościć funkcję jakbym miał zrobić np dla 100 zdjęć abym nie musiał tworzyć dla każdego zdjecia

  1. <!DOCTYPE html>
  2. <meta name="viewport" content="width=device-width, initial-scale=1">
  3. body {font-family: Arial, Helvetica, sans-serif;}
  4.  
  5. #myImg {
  6. border-radius: 5px;
  7. cursor: pointer;
  8. transition: 0.3s;
  9. }
  10.  
  11. #myImg:hover {opacity: 0.7;}
  12.  
  13. #myImg2 {
  14. border-radius: 5px;
  15. cursor: pointer;
  16. transition: 0.3s;
  17. }
  18.  
  19. #myImg2:hover {opacity: 0.7;}
  20.  
  21.  
  22.  
  23. /* The Modal (background) */
  24. .modal {
  25. display: none; /* Hidden by default */
  26. position: fixed; /* Stay in place */
  27. z-index: 1; /* Sit on top */
  28. padding-top: 100px; /* Location of the box */
  29. left: 0;
  30. top: 0;
  31. width: 100%; /* Full width */
  32. height: 100%; /* Full height */
  33. overflow: auto; /* Enable scroll if needed */
  34. background-color: rgb(0,0,0); /* Fallback color */
  35. background-color: rgba(0,0,0,0.9); /* Black w/ opacity */
  36. }
  37.  
  38. /* Modal Content (image) */
  39. .modal-content {
  40. margin: auto;
  41. display: block;
  42. width: 80%;
  43. max-width: 700px;
  44. }
  45.  
  46. /* Caption of Modal Image */
  47. #caption {
  48. margin: auto;
  49. display: block;
  50. width: 80%;
  51. max-width: 700px;
  52. text-align: center;
  53. color: #ccc;
  54. padding: 10px 0;
  55. height: 150px;
  56. }
  57.  
  58. /* Add Animation */
  59. .modal-content, #caption {
  60. -webkit-animation-name: zoom;
  61. -webkit-animation-duration: 0.6s;
  62. animation-name: zoom;
  63. animation-duration: 0.6s;
  64. }
  65.  
  66. @-webkit-keyframes zoom {
  67. from {-webkit-transform:scale(0)}
  68. to {-webkit-transform:scale(1)}
  69. }
  70.  
  71. @keyframes zoom {
  72. from {transform:scale(0)}
  73. to {transform:scale(1)}
  74. }
  75.  
  76. /* The Close Button */
  77. .close {
  78. position: absolute;
  79. top: 15px;
  80. right: 35px;
  81. color: #f1f1f1;
  82. font-size: 40px;
  83. font-weight: bold;
  84. transition: 0.3s;
  85. }
  86.  
  87. .close:hover,
  88. .close:focus {
  89. color: #bbb;
  90. text-decoration: none;
  91. cursor: pointer;
  92. }
  93.  
  94. /* 100% Image Width on Smaller Screens */
  95. @media only screen and (max-width: 700px){
  96. .modal-content {
  97. width: 100%;
  98. }
  99. }
  100. </head>
  101.  
  102. <h2>Image Modal</h2>
  103. <p>In this example, we use CSS to create a modal (dialog box) that is hidden by default.</p>
  104. <p>We use JavaScript to trigger the modal and to display the current image inside the modal when it is clicked on. Also note that we use the value from the image's "alt" attribute as an image caption text inside the modal.</p>
  105.  
  106. <img id="myImg" src="img_snow.jpg" alt="Snow1" style="width:100%;max-width:300px">
  107. <img id="myImg2" src="7.jpg" alt="Snow2" style="width:100%;max-width:300px">
  108.  
  109. <!-- The Modal -->
  110. <div id="myModal" class="modal">
  111. <span class="close">&times;</span>
  112. <img class="modal-content" id="img01">
  113. <div id="caption"></div>
  114. </div>
  115.  
  116.  
  117. // Get the modal
  118. var modal = document.getElementById('myModal');
  119.  
  120. // Get the image and insert it inside the modal - use its "alt" text as a caption
  121. var img = document.getElementById('myImg');
  122. var modalImg = document.getElementById("img01");
  123. var captionText = document.getElementById("caption");
  124. img.onclick = function(){
  125. modal.style.display = "block";
  126. modalImg.src = this.src;
  127. captionText.innerHTML = this.alt;
  128. }
  129.  
  130. // Get the <span> element that closes the modal
  131. var span = document.getElementsByClassName("close")[0];
  132.  
  133. // When the user clicks on <span> (x), close the modal
  134. span.onclick = function() {
  135. modal.style.display = "none";
  136. }
  137.  
  138.  
  139. // Get the modal
  140. var modal = document.getElementById('myModal');
  141.  
  142. // Get the image and insert it inside the modal - use its "alt" text as a caption
  143. var img = document.getElementById('myImg2');
  144. var modalImg = document.getElementById("img01");
  145. var captionText = document.getElementById("caption");
  146. img.onclick = function(){
  147. modal.style.display = "block";
  148. modalImg.src = this.src;
  149. captionText.innerHTML = this.alt;
  150. }
  151.  
  152. // Get the <span> element that closes the modal
  153. var span = document.getElementsByClassName("close")[0];
  154.  
  155. // When the user clicks on <span> (x), close the modal
  156. span.onclick = function() {
  157. modal.style.display = "none";
  158. }
  159. </body>
  160. </html>
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 19.08.2025 - 16:06