Advertisement
Guest User

Untitled

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