Guest User

Basic Modal Demo

a guest
Jun 15th, 2019
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. <?php
  2. $episodes = [
  3. [
  4. 'episode_title' => 'Lorem Ipsum',
  5. 'description' => "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, whe<p>n an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."],
  6. [
  7. 'episode_title' => "The standard 'Lorem Ipsum' passage, used since the 1500s",
  8. 'description' => '"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud <p>exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."'
  9. ]
  10. ];
  11. ?>
  12. <!DOCTYPE html>
  13. <html>
  14. <head>
  15. <meta name="viewport" content="width=device-width, initial-scale=1">
  16. <style>
  17. body {font-family: Arial, Helvetica, sans-serif;}
  18.  
  19. .box {
  20. padding: 50px;
  21. border: solid 2px black;
  22. margin: 50px;
  23. }
  24.  
  25. .triggersModal {
  26. cursor: pointer;
  27. }
  28.  
  29. /* The Modal (background) */
  30. #myModal {
  31. display: none; /* Hidden by default */
  32. position: fixed; /* Stay in place */
  33. z-index: 1; /* Sit on top */
  34. padding-top: 100px; /* Location of the box */
  35. left: 0;
  36. top: 0;
  37. width: 100%; /* Full width */
  38. height: 100%; /* Full height */
  39. overflow: auto; /* Enable scroll if needed */
  40. background-color: rgb(0,0,0); /* Fallback color */
  41. background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
  42. }
  43.  
  44. /* Modal Content */
  45. .modal-content {
  46. background-color: #fefefe;
  47. margin: auto;
  48. padding: 20px;
  49. border: 1px solid #888;
  50. width: 80%;
  51. }
  52.  
  53. /* The Close Button */
  54. #modalCloser {
  55. color: #aaaaaa;
  56. float: right;
  57. font-size: 28px;
  58. font-weight: bold;
  59. }
  60.  
  61. #modalCloser:hover,
  62. #modalCloser:focus {
  63. color: #000;
  64. text-decoration: none;
  65. cursor: pointer;
  66. }
  67. </style>
  68. </head>
  69. <body>
  70.  
  71. <?php
  72. foreach ($episodes as $index => $episode) {
  73. echo "<div class=\"box\">";
  74. echo "<div class=\"triggersModal\" data-index=\"{$index}\" data-content=\"episode_title\">";
  75. echo strlen($episode['episode_title']) <= 50
  76. ? htmlspecialchars($episode['episode_title'])
  77. : htmlspecialchars(substr($episode['episode_title'], 0, 50)) , "(...)";
  78. echo "</div>";
  79. echo "<div class=\"triggersModal\" data-index=\"{$index}\" data-content=\"description\">";
  80. echo strlen($episode['description']) <= 100
  81. ? htmlspecialchars($episode['description'])
  82. : htmlspecialchars(substr($episode['description'], 0, 100)) , "(...)";
  83. echo "</div>";
  84. echo "</div>";
  85. $episodes[$index] = ['episode_title' => htmlspecialchars($episode['episode_title']), 'description' => htmlspecialchars($episode['description'])];
  86. }
  87. ?>
  88.  
  89. <!-- The Modal -->
  90. <div id="myModal" class="modal">
  91. <!-- Modal content -->
  92. <div class="modal-content">
  93. <span id="modalCloser">&times;</span>
  94. <p id="modalText"></p>
  95. </div>
  96. </div>
  97.  
  98. <script>
  99. // Transfer data from php to javascript
  100. let lookup = <?php echo json_encode($episodes); ?>,
  101. classname = document.getElementsByClassName("triggersModal"),
  102. modal = document.getElementById("myModal");
  103.  
  104. // Bind value insertion and modal display to onclick event of every element with named class
  105. for (let i = 0, len = classname.length; i < len; ++i) {
  106. classname[i].onclick = function() {
  107. document.getElementById("modalText").innerHTML = lookup[this.getAttribute('data-index')][this.getAttribute('data-content')];
  108. modal.style.display = "block";
  109. }
  110. }
  111.  
  112. // Close the modal
  113. document.getElementById("modalCloser").onclick = function() {
  114. modal.style.display = "none";
  115. }
  116.  
  117. // When the user clicks anywhere outside of the modal, close it
  118. window.onclick = function(event) {
  119. if (event.target == modal) {
  120. modal.style.display = "none";
  121. }
  122. }
  123. </script>
  124.  
  125. </body>
  126. </html>
Advertisement
Add Comment
Please, Sign In to add comment