如何从表格中读取图像,并在按钮单击时在弹出窗口中以全尺寸显示?

huangapple go评论103阅读模式
英文:

How to read image from table and on button click show it full size, inside popup window?

问题

  1. Explanation (1)
    我有一个图像/图片列表,这些图片位于页面中的一个表格中。每个图像/图片都有一个唯一的名称,并且成功显示在表格中。
    我想要的是,在单击按钮时,查看我选择的图像/图片,以全屏显示在弹出窗口中。

  2. Question (2)
    如何在弹出窗口中显示图片?

  3. The frontend code (3)

  1. // 表格
  2. <table id="table1" class="border-0" style="width: 100%; text-align: left;">
  3. <thead class="border-0">
  4. <tr class="text-dark">
  5. <th style="width:10%;">
  6. Data
  7. </th>
  8. <th style="width:80%;">
  9. Explanation
  10. </th>
  11. <th style="width:1%; text-align:right;">
  12. Image
  13. </th>
  14. </tr>
  15. </thead>
  16. <tbody>
  17. @foreach (var item in Model.ImgList)
  18. {
  19. <tr class="border-bottom">
  20. <td>
  21. @item.ImgDate.ToShortDateString()
  22. </td>
  23. <td>
  24. @Html.DisplayFor(modelItem => item.Text)
  25. </td>
  26. <td>
  27. <a href="#myModal" class="btn btn-sm btn-dark font-weight-bold border-0"
  28. data-id="@item.MyImgId" data-imgid="@(item.MyImgId)" data-toggle="modal" data-bs-toggle="modal" style="min-width:80px;">
  29. <img src="~/grafi/@(item.MyImgId + ".png")" height="50" alt="image" id="myImg" />
  30. </a>
  31. </td>
  32. </tr>
  33. }
  34. </tbody>
  35. </table>
  36. // 弹出窗口
  37. <div id="myModal" class="modal">
  38. <span class="close">&times;</span>
  39. <img id="img01" style="height:100vh" />
  40. </div>
  41. // 脚本
  42. var modal = document.getElementById("myModal");
  43. var img = document.getElementById("myImg");
  44. var modalImg = document.getElementById("img01");
  45. var captionText = document.getElementById("caption");
  46. img.onclick = function() {
  47. modal.style display = "block";
  48. modalImg.src = this.src;
  49. captionText.innerHTML = this.alt;
  50. }
  51. var span = document.getElementsByClassName("close")[0];
  52. span.onclick = function() {
  53. modal.style.display = "none";
  54. }
英文:

1. Explaination

I have a list of images/picture that are listen inside a page through a table.
Each image/picture has a unique name and is successfully shown in the table.

What i want is to view the one i choose, in full screen, inside a popup window, on button click.

2. Question

How can i show the picture inside a popup window?

3. The frontend code

// The table

  1. &lt;table id=&quot;table1&quot; class=&quot;border-0&quot;
  2. style=&quot;width: 100%;text-align: left;&quot;&gt;
  3. &lt;thead class=&quot;border-0&quot;&gt;
  4. &lt;tr class=&quot;text-dark&quot;&gt;
  5. &lt;th style=&quot;width:10%;&quot;&gt;
  6. Data
  7. &lt;/th&gt;
  8. &lt;th style=&quot;width:80%;&quot;&gt;
  9. Explaination
  10. &lt;/th&gt;
  11. &lt;th style=&quot;width:1%;text-align:right;&quot;&gt;
  12. Image
  13. &lt;/th&gt;
  14. &lt;/tr&gt;
  15. &lt;/thead&gt;
  16. &lt;tbody&gt;
  17. @foreach (var item in Model.ImgList)
  18. {
  19. &lt;tr class=&quot;border-bottom&quot;&gt;
  20. &lt;td&gt;
  21. @item.ImgDate.ToShortDateString()
  22. &lt;/td&gt;
  23. &lt;td&gt;
  24. @Html.DisplayFor(modelItem =&gt; item.Text)
  25. &lt;/td&gt;
  26. &lt;td&gt;
  27. &lt;a href=&quot;#myModal&quot; class=&quot;btn btn-sm btn-dark font-weight-bold border-0&quot;
  28. data-id=&quot;@item.MyImgId&quot; data-imgid=&quot;@(item.MyImgId)&quot; data-toggle=&quot;modal&quot; data-bs-toggle=&quot;modal&quot; style=&quot;min-width:80px;&quot;&gt;
  29. &lt;img src=&quot;~/grafi/@(item.MyImgId + &quot;.png&quot;)&quot; height=&quot;50&quot; alt=&quot;image&quot; id=&quot;myImg&quot; /&gt;
  30. &lt;/a&gt;
  31. &lt;/td&gt;
  32. &lt;/tr&gt;
  33. }
  34. &lt;/tbody&gt;
  35. &lt;/table&gt;

//The popup modal

  1. &lt;!-- The Modal --&gt;
  2. &lt;div id=&quot;myModal&quot; class=&quot;modal&quot;&gt;
  3. &lt;span class=&quot;close&quot;&gt;&amp;times;&lt;/span&gt;
  4. &lt;img id=&quot;img01&quot; style=&quot;height:100vh&quot; /&gt;
  5. &lt;/div&gt;

//The scripts

  1. var modal = document.getElementById(&quot;myModal&quot;);
  2. var img = document.getElementById(&quot;myImg&quot;);
  3. var modalImg = document.getElementById(&quot;img01&quot;);
  4. var captionText = document.getElementById(&quot;caption&quot;);
  5. img.onclick = function(){
  6. modal.style.display = &quot;block&quot;;
  7. modalImg.src = this.src;
  8. captionText.innerHTML = this.alt;
  9. }
  10. var span = document.getElementsByClassName(&quot;close&quot;)[0];
  11. span.onclick = function() {
  12. modal.style.display = &quot;none&quot;;
  13. }

答案1

得分: 1

以下是您要翻译的代码部分:

  1. Just moved it in a table. Seems to work fine:
  2. <!-- begin snippet: js hide: false console: true babel: false -->
  3. <!-- language: lang-js -->
  4. // Get the modal
  5. var modal = document.getElementById("myModal");
  6. console.log(modal);
  7. // get the modal image
  8. var caption = document.getElementById("modalImg");
  9. // get the caption span
  10. var caption = document.getElementById("caption");
  11. // Get the <span> element that closes the modal
  12. var close = document.getElementById("close");
  13. // When the user clicks on <span> (x), close the modal
  14. close.onclick = function() {
  15. modal.style display = "none";
  16. }
  17. const images = document.getElementsByClassName('image');
  18. for (let image of images) {
  19. image.onclick = function(event) {
  20. image = event.target;
  21. modal.style display = "block";
  22. modalImg.src = image.src;
  23. caption.innertext = image.alt;
  24. }
  25. }
  26. <!-- language: lang-css -->
  27. body {
  28. font-family: Arial, Helvetica, sans-serif;
  29. }
  30. .image {
  31. border-radius: 5px;
  32. cursor: pointer;
  33. transition: 0.3s;
  34. }
  35. .image:hover {
  36. opacity: 0.7;
  37. }
  38. /* The Modal (background) */
  39. .modal {
  40. display: none;
  41. /* Hidden by default */
  42. position: fixed;
  43. /* Stay in place */
  44. z-index: 1;
  45. /* Sit on top */
  46. padding-top: 100px;
  47. /* Location of the box */
  48. left: 0;
  49. top: 0;
  50. width: 100%;
  51. /* Full width */
  52. height: 100%;
  53. /* Full height */
  54. overflow: auto;
  55. /* Enable scroll if needed */
  56. background-color: rgb(0, 0, 0);
  57. /* Fallback color */
  58. background-color: rgba(0, 0, 0, 0.9);
  59. /* Black w/ opacity */
  60. }
  61. /* Modal Content (image) */
  62. .modal-content {
  63. margin: auto;
  64. display: block;
  65. width: 80%;
  66. max-width: 700px;
  67. }
  68. /* Caption of Modal Image */
  69. #caption {
  70. margin: auto;
  71. display: block;
  72. width: 80%;
  73. max-width: 700px;
  74. text-align: center;
  75. color: #ccc;
  76. padding: 10px 0;
  77. height: 150px;
  78. }
  79. /* Add Animation */
  80. .modal-content,
  81. #caption {
  82. -webkit-animation-name: zoom;
  83. -webkit-animation-duration: 0.6s;
  84. animation-name: zoom;
  85. animation-duration: 0.6s;
  86. }
  87. @-webkit-keyframes zoom {
  88. from {
  89. -webkit-transform: scale(0)
  90. }
  91. to {
  92. -webkit-transform: scale(1)
  93. }
  94. }
  95. @keyframes zoom {
  96. from {
  97. transform: scale(0)
  98. }
  99. to {
  100. transform: scale(1)
  101. }
  102. }
  103. /* The Close Button */
  104. .close {
  105. position: absolute;
  106. top: 15px;
  107. right: 35px;
  108. color: #f1f1f1;
  109. font-size: 40px;
  110. font-weight: bold;
  111. transition: 0.3s;
  112. }
  113. .close:hover,
  114. .close:focus {
  115. color: #bbb;
  116. text-decoration: none;
  117. cursor: pointer;
  118. }
  119. /* 100% Image Width on Smaller Screens */
  120. @media only screen and (max-width: 700px) {
  121. .modal-content {
  122. width: 100%;
  123. }
  124. }
  125. <!-- language: lang-html -->
  126. <html>
  127. <head>
  128. <meta name="viewport" content="width=device-width, initial-scale=1">
  129. </head>
  130. <body>
  131. <h2>Image Modal</h2>
  132. <p>In this example, we use CSS to create a modal (dialog box) that is hidden by default.</p>
  133. <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.
  134. <table id="table1" class="border-0" style="width: 100%;text-align: left;">
  135. <thead class="border-0">
  136. <tr class="text-dark">
  137. <th style="width:10%;">
  138. Data
  139. </th>
  140. <th style="width:80%;">
  141. Explanation
  142. </th>
  143. <th style="width:1%;text-align:right;">
  144. Image
  145. </th>
  146. </tr>
  147. </thead>
  148. <tbody>
  149. <tr class="border-bottom">
  150. <td>
  151. Image
  152. </td>
  153. <td>
  154. Some text
  155. </td>
  156. <td>
  157. <img class="image" src="https://www.w3schools.com/howto/img_snow.jpg" alt="Snow" style="width:100%;max-width:300px">
  158. </td>
  159. </tr>
  160. <tr class="border-bottom">
  161. <td>
  162. Image
  163. </td>
  164. <td>
  165. Some text
  166. </td>
  167. <td>
  168. <img class="image" src="https://www.w3schools.com/howto/img_snow.jpg" alt="Snow" style="width:100%;max-width:300px">
  169. </td>
  170. </tr>
  171. </tbody>
  172. </table>
  173. <!-- The Modal -->
  174. <div id="myModal" class="modal">
  175. <span id="close" class="close">&times;</span>
  176. <img class="modal-content" id="modalImg">
  177. <div id="caption"></div>
  178. </div>
  179. </body>
  180. </html>
  181. <!-- end snippet -->

希望这对您有所帮助!

英文:

Just moved it in a table. Seems to work fine:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. // Get the modal
  2. var modal = document.getElementById(&quot;myModal&quot;);
  3. console.log(modal);
  4. // get the modal image
  5. var caption = document.getElementById(&quot;modalImg&quot;);
  6. // get the caption span
  7. var caption = document.getElementById(&quot;caption&quot;);
  8. // Get the &lt;span&gt; element that closes the modal
  9. var close = document.getElementById(&quot;close&quot;);
  10. // When the user clicks on &lt;span&gt; (x), close the modal
  11. close.onclick = function() {
  12. modal.style.display = &quot;none&quot;;
  13. }
  14. const images = document.getElementsByClassName(&#39;image&#39;);
  15. for (let image of images) {
  16. image.onclick = function(event) {
  17. image = event.target;
  18. modal.style.display = &quot;block&quot;;
  19. modalImg.src = image.src;
  20. caption.innertext = image.alt;
  21. }
  22. }

<!-- language: lang-css -->

  1. body {
  2. font-family: Arial, Helvetica, sans-serif;
  3. }
  4. .image {
  5. border-radius: 5px;
  6. cursor: pointer;
  7. transition: 0.3s;
  8. }
  9. .image:hover {
  10. opacity: 0.7;
  11. }
  12. /* The Modal (background) */
  13. .modal {
  14. display: none;
  15. /* Hidden by default */
  16. position: fixed;
  17. /* Stay in place */
  18. z-index: 1;
  19. /* Sit on top */
  20. padding-top: 100px;
  21. /* Location of the box */
  22. left: 0;
  23. top: 0;
  24. width: 100%;
  25. /* Full width */
  26. height: 100%;
  27. /* Full height */
  28. overflow: auto;
  29. /* Enable scroll if needed */
  30. background-color: rgb(0, 0, 0);
  31. /* Fallback color */
  32. background-color: rgba(0, 0, 0, 0.9);
  33. /* Black w/ opacity */
  34. }
  35. /* Modal Content (image) */
  36. .modal-content {
  37. margin: auto;
  38. display: block;
  39. width: 80%;
  40. max-width: 700px;
  41. }
  42. /* Caption of Modal Image */
  43. #caption {
  44. margin: auto;
  45. display: block;
  46. width: 80%;
  47. max-width: 700px;
  48. text-align: center;
  49. color: #ccc;
  50. padding: 10px 0;
  51. height: 150px;
  52. }
  53. /* Add Animation */
  54. .modal-content,
  55. #caption {
  56. -webkit-animation-name: zoom;
  57. -webkit-animation-duration: 0.6s;
  58. animation-name: zoom;
  59. animation-duration: 0.6s;
  60. }
  61. @-webkit-keyframes zoom {
  62. from {
  63. -webkit-transform: scale(0)
  64. }
  65. to {
  66. -webkit-transform: scale(1)
  67. }
  68. }
  69. @keyframes zoom {
  70. from {
  71. transform: scale(0)
  72. }
  73. to {
  74. transform: scale(1)
  75. }
  76. }
  77. /* The Close Button */
  78. .close {
  79. position: absolute;
  80. top: 15px;
  81. right: 35px;
  82. color: #f1f1f1;
  83. font-size: 40px;
  84. font-weight: bold;
  85. transition: 0.3s;
  86. }
  87. .close:hover,
  88. .close:focus {
  89. color: #bbb;
  90. text-decoration: none;
  91. cursor: pointer;
  92. }
  93. /* 100% Image Width on Smaller Screens */
  94. @media only screen and (max-width: 700px) {
  95. .modal-content {
  96. width: 100%;
  97. }
  98. }

<!-- language: lang-html -->

  1. &lt;html&gt;
  2. &lt;head&gt;
  3. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
  4. &lt;/head&gt;
  5. &lt;body&gt;
  6. &lt;h2&gt;Image Modal&lt;/h2&gt;
  7. &lt;p&gt;In this example, we use CSS to create a modal (dialog box) that is hidden by default.&lt;/p&gt;
  8. &lt;p&gt;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&#39;s &quot;alt&quot; attribute as an image caption text inside the modal.&lt;/p&gt;
  9. &lt;table id=&quot;table1&quot; class=&quot;border-0&quot; style=&quot;width: 100%;text-align: left;&quot;&gt;
  10. &lt;thead class=&quot;border-0&quot;&gt;
  11. &lt;tr class=&quot;text-dark&quot;&gt;
  12. &lt;th style=&quot;width:10%;&quot;&gt;
  13. Data
  14. &lt;/th&gt;
  15. &lt;th style=&quot;width:80%;&quot;&gt;
  16. Explaination
  17. &lt;/th&gt;
  18. &lt;th style=&quot;width:1%;text-align:right;&quot;&gt;
  19. Image
  20. &lt;/th&gt;
  21. &lt;/tr&gt;
  22. &lt;/thead&gt;
  23. &lt;tbody&gt;
  24. &lt;tr class=&quot;border-bottom&quot;&gt;
  25. &lt;td&gt;
  26. image
  27. &lt;/td&gt;
  28. &lt;td&gt;
  29. some text
  30. &lt;/td&gt;
  31. &lt;td&gt;
  32. &lt;img class=&quot;image&quot; src=&quot;https://www.w3schools.com/howto/img_snow.jpg&quot; alt=&quot;Snow&quot; style=&quot;width:100%;max-width:300px&quot;&gt;
  33. &lt;/td&gt;
  34. &lt;/tr&gt;
  35. &lt;tr class=&quot;border-bottom&quot;&gt;
  36. &lt;td&gt;
  37. image
  38. &lt;/td&gt;
  39. &lt;td&gt;
  40. some text
  41. &lt;/td&gt;
  42. &lt;td&gt;
  43. &lt;img class=&quot;image&quot; src=&quot;https://www.w3schools.com/howto/img_snow.jpg&quot; alt=&quot;Snow&quot; style=&quot;width:100%;max-width:300px&quot;&gt;
  44. &lt;/td&gt;
  45. &lt;/tr&gt;
  46. &lt;/tbody&gt;
  47. &lt;/table&gt;
  48. &lt;!-- The Modal --&gt;
  49. &lt;div id=&quot;myModal&quot; class=&quot;modal&quot;&gt;
  50. &lt;span id=&quot;close&quot; class=&quot;close&quot;&gt;&amp;times;&lt;/span&gt;
  51. &lt;img class=&quot;modal-content&quot; id=&quot;modalImg&quot;&gt;
  52. &lt;div id=&quot;caption&quot;&gt;&lt;/div&gt;
  53. &lt;/div&gt;
  54. &lt;/body&gt;
  55. &lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月4日 17:10:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75927523.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定