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

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

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)

// 表格
<table id="table1" class="border-0" style="width: 100%; text-align: left;">
    <thead class="border-0">
        <tr class="text-dark">
            <th style="width:10%;">
                Data
            </th>
            <th style="width:80%;">
                Explanation
            </th>
            <th style="width:1%; text-align:right;">
                Image
            </th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.ImgList)
        {
            <tr class="border-bottom">
                <td>
                    @item.ImgDate.ToShortDateString()
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Text)
                </td>
                <td>
                    <a href="#myModal" class="btn btn-sm btn-dark font-weight-bold border-0"
                       data-id="@item.MyImgId" data-imgid="@(item.MyImgId)" data-toggle="modal" data-bs-toggle="modal" style="min-width:80px;">
                        <img src="~/grafi/@(item.MyImgId + ".png")" height="50" alt="image" id="myImg" />
                    </a>
                </td>
            </tr>
        }
    </tbody>
</table>

// 弹出窗口
<div id="myModal" class="modal">
    <span class="close">&times;</span>
    <img id="img01" style="height:100vh" />
</div>

// 脚本
var modal = document.getElementById("myModal");
var img = document.getElementById("myImg");
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");

img.onclick = function() {
    modal.style display = "block";
    modalImg.src = this.src;
    captionText.innerHTML = this.alt;
}

var span = document.getElementsByClassName("close")[0];
span.onclick = function() {
    modal.style.display = "none";
}
英文:

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

  &lt;table id=&quot;table1&quot; class=&quot;border-0&quot;
style=&quot;width: 100%;text-align: left;&quot;&gt;
&lt;thead class=&quot;border-0&quot;&gt;
&lt;tr class=&quot;text-dark&quot;&gt;                          
&lt;th style=&quot;width:10%;&quot;&gt;
Data
&lt;/th&gt;                           
&lt;th style=&quot;width:80%;&quot;&gt;
Explaination
&lt;/th&gt;                            
&lt;th style=&quot;width:1%;text-align:right;&quot;&gt;
Image                             
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
@foreach (var item in Model.ImgList)
{
&lt;tr class=&quot;border-bottom&quot;&gt;                               
&lt;td&gt;                             
@item.ImgDate.ToShortDateString()
&lt;/td&gt;                              
&lt;td&gt;
@Html.DisplayFor(modelItem =&gt; item.Text)
&lt;/td&gt;
&lt;td&gt;
&lt;a href=&quot;#myModal&quot; class=&quot;btn btn-sm btn-dark font-weight-bold border-0&quot;
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;
&lt;img src=&quot;~/grafi/@(item.MyImgId + &quot;.png&quot;)&quot; height=&quot;50&quot; alt=&quot;image&quot; id=&quot;myImg&quot; /&gt;
&lt;/a&gt;                                                                     
&lt;/td&gt;                           
&lt;/tr&gt;
}
&lt;/tbody&gt;
&lt;/table&gt;

//The popup modal

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

//The scripts

var modal = document.getElementById(&quot;myModal&quot;);
var img = document.getElementById(&quot;myImg&quot;);
var modalImg = document.getElementById(&quot;img01&quot;);
var captionText = document.getElementById(&quot;caption&quot;);
img.onclick = function(){
modal.style.display = &quot;block&quot;;
modalImg.src = this.src;
captionText.innerHTML = this.alt;
}
var span = document.getElementsByClassName(&quot;close&quot;)[0];
span.onclick = function() {
modal.style.display = &quot;none&quot;;
}

答案1

得分: 1

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

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

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

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

    // Get the modal
    var modal = document.getElementById("myModal");
    console.log(modal);
    // get the modal image
    var caption = document.getElementById("modalImg");
    // get the caption span
    var caption = document.getElementById("caption");
    // Get the <span> element that closes the modal
    var close = document.getElementById("close");
    // When the user clicks on <span> (x), close the modal
    close.onclick = function() {
      modal.style display = "none";
    }
    const images = document.getElementsByClassName('image');
    for (let image of images) {
      image.onclick = function(event) {
        image = event.target;
        modal.style display = "block";
        modalImg.src = image.src;
        caption.innertext = image.alt;
      }
    }

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

    body {
          font-family: Arial, Helvetica, sans-serif;
        }
        
        .image {
          border-radius: 5px;
          cursor: pointer;
          transition: 0.3s;
        }
        
        .image:hover {
          opacity: 0.7;
        }
        /* The Modal (background) */
        
        .modal {
          display: none;
          /* Hidden by default */
          position: fixed;
          /* Stay in place */
          z-index: 1;
          /* Sit on top */
          padding-top: 100px;
          /* Location of the box */
          left: 0;
          top: 0;
          width: 100%;
          /* Full width */
          height: 100%;
          /* Full height */
          overflow: auto;
          /* Enable scroll if needed */
          background-color: rgb(0, 0, 0);
          /* Fallback color */
          background-color: rgba(0, 0, 0, 0.9);
          /* Black w/ opacity */
        }
        /* Modal Content (image) */
        
        .modal-content {
          margin: auto;
          display: block;
          width: 80%;
          max-width: 700px;
        }
        /* Caption of Modal Image */
        
        #caption {
          margin: auto;
          display: block;
          width: 80%;
          max-width: 700px;
          text-align: center;
          color: #ccc;
          padding: 10px 0;
          height: 150px;
        }
        /* Add Animation */
        
        .modal-content,
        #caption {
          -webkit-animation-name: zoom;
          -webkit-animation-duration: 0.6s;
          animation-name: zoom;
          animation-duration: 0.6s;
        }
        
        @-webkit-keyframes zoom {
          from {
            -webkit-transform: scale(0)
          }
          to {
            -webkit-transform: scale(1)
          }
        }
        
        @keyframes zoom {
          from {
            transform: scale(0)
          }
          to {
            transform: scale(1)
          }
        }
        /* The Close Button */
        
        .close {
          position: absolute;
          top: 15px;
          right: 35px;
          color: #f1f1f1;
          font-size: 40px;
          font-weight: bold;
          transition: 0.3s;
        }
        
        .close:hover,
        .close:focus {
          color: #bbb;
          text-decoration: none;
          cursor: pointer;
        }
        /* 100% Image Width on Smaller Screens */
        
        @media only screen and (max-width: 700px) {
          .modal-content {
            width: 100%;
          }
        }

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

    <html>

    <head>
      <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>

    <body>

      <h2>Image Modal</h2>
      <p>In this example, we use CSS to create a modal (dialog box) that is hidden by default.</p>
      <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.


      <table id="table1" class="border-0" style="width: 100%;text-align: left;">
        <thead class="border-0">
          <tr class="text-dark">
            <th style="width:10%;">
              Data
            </th>
            <th style="width:80%;">
              Explanation
            </th>
            <th style="width:1%;text-align:right;">
              Image
            </th>
          </tr>
        </thead>
        <tbody>
          <tr class="border-bottom">
            <td>
              Image
            </td>
            <td>
              Some text
            </td>
            <td>
              <img class="image" src="https://www.w3schools.com/howto/img_snow.jpg" alt="Snow" style="width:100%;max-width:300px">
            </td>
          </tr>
          <tr class="border-bottom">
            <td>
              Image
            </td>
            <td>
              Some text
            </td>
            <td>
              <img class="image" src="https://www.w3schools.com/howto/img_snow.jpg" alt="Snow" style="width:100%;max-width:300px">
            </td>
          </tr>
        </tbody>
      </table>
      <!-- The Modal -->
      <div id="myModal" class="modal">
        <span id="close" class="close">&times;</span>
        <img class="modal-content" id="modalImg">
        <div id="caption"></div>
      </div>
    </body>

    </html>

<!-- end snippet -->

希望这对您有所帮助!

英文:

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

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

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

// Get the modal
var modal = document.getElementById(&quot;myModal&quot;);
console.log(modal);
// get the modal image
var caption = document.getElementById(&quot;modalImg&quot;);
// get the caption span
var caption = document.getElementById(&quot;caption&quot;);
// Get the &lt;span&gt; element that closes the modal
var close = document.getElementById(&quot;close&quot;);
// When the user clicks on &lt;span&gt; (x), close the modal
close.onclick = function() {
modal.style.display = &quot;none&quot;;
}
const images = document.getElementsByClassName(&#39;image&#39;);
for (let image of images) {
image.onclick = function(event) {
image = event.target;
modal.style.display = &quot;block&quot;;
modalImg.src = image.src;
caption.innertext = image.alt;
}
}

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

body {
font-family: Arial, Helvetica, sans-serif;
}
.image {
border-radius: 5px;
cursor: pointer;
transition: 0.3s;
}
.image:hover {
opacity: 0.7;
}
/* The Modal (background) */
.modal {
display: none;
/* Hidden by default */
position: fixed;
/* Stay in place */
z-index: 1;
/* Sit on top */
padding-top: 100px;
/* Location of the box */
left: 0;
top: 0;
width: 100%;
/* Full width */
height: 100%;
/* Full height */
overflow: auto;
/* Enable scroll if needed */
background-color: rgb(0, 0, 0);
/* Fallback color */
background-color: rgba(0, 0, 0, 0.9);
/* Black w/ opacity */
}
/* Modal Content (image) */
.modal-content {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
}
/* Caption of Modal Image */
#caption {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
text-align: center;
color: #ccc;
padding: 10px 0;
height: 150px;
}
/* Add Animation */
.modal-content,
#caption {
-webkit-animation-name: zoom;
-webkit-animation-duration: 0.6s;
animation-name: zoom;
animation-duration: 0.6s;
}
@-webkit-keyframes zoom {
from {
-webkit-transform: scale(0)
}
to {
-webkit-transform: scale(1)
}
}
@keyframes zoom {
from {
transform: scale(0)
}
to {
transform: scale(1)
}
}
/* The Close Button */
.close {
position: absolute;
top: 15px;
right: 35px;
color: #f1f1f1;
font-size: 40px;
font-weight: bold;
transition: 0.3s;
}
.close:hover,
.close:focus {
color: #bbb;
text-decoration: none;
cursor: pointer;
}
/* 100% Image Width on Smaller Screens */
@media only screen and (max-width: 700px) {
.modal-content {
width: 100%;
}
}

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

&lt;html&gt;
&lt;head&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h2&gt;Image Modal&lt;/h2&gt;
&lt;p&gt;In this example, we use CSS to create a modal (dialog box) that is hidden by default.&lt;/p&gt;
&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;
&lt;table id=&quot;table1&quot; class=&quot;border-0&quot; style=&quot;width: 100%;text-align: left;&quot;&gt;
&lt;thead class=&quot;border-0&quot;&gt;
&lt;tr class=&quot;text-dark&quot;&gt;
&lt;th style=&quot;width:10%;&quot;&gt;
Data
&lt;/th&gt;
&lt;th style=&quot;width:80%;&quot;&gt;
Explaination
&lt;/th&gt;
&lt;th style=&quot;width:1%;text-align:right;&quot;&gt;
Image
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr class=&quot;border-bottom&quot;&gt;
&lt;td&gt;
image
&lt;/td&gt;
&lt;td&gt;
some text
&lt;/td&gt;
&lt;td&gt;
&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;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&quot;border-bottom&quot;&gt;
&lt;td&gt;
image
&lt;/td&gt;
&lt;td&gt;
some text
&lt;/td&gt;
&lt;td&gt;
&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;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;!-- The Modal --&gt;
&lt;div id=&quot;myModal&quot; class=&quot;modal&quot;&gt;
&lt;span id=&quot;close&quot; class=&quot;close&quot;&gt;&amp;times;&lt;/span&gt;
&lt;img class=&quot;modal-content&quot; id=&quot;modalImg&quot;&gt;
&lt;div id=&quot;caption&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&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:

确定