如何从一张图片制作一个模态窗口?

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

How to make a modal window from an image?

问题

I have the following HTML:

<div class="row">
  <div class="col-lg-3 col-md-4 col-sm-6 col-xs-12 text-center wow fadeIn">
    <a href="assets/img/our_works/big_img/1.png">
      <img class="preview" src="assets/img/our_works/1.png" alt="window">
    </a>
  </div>
  <div class="col-lg-3 col-md-4 col-sm-6 col-xs-12 text-center wow fadeIn" data-wow-delay="0.1s">
    <a href="assets/img/our_works/big_img/2.png">
      <img class="preview" src="assets/img/our_works/2.png" alt="window">
    </a>
  </div>
</div>

I need to write a JS script so that when a user clicks on an image modal window appears with a bigger image (big_img/1.png) with a half-transparent background.

I wrote this code:

const previewImages = document.querySelectorAll(".preview");

previewImages.forEach((previewImage) => {

  previewImage.addEventListener("click", (event) => {

    event.preventDefault();

    const imagePath = event.target.src;
    const bigImagePath = imagePath.replace("our_works", "our_works/big_img");

but then stuck on how to proceed without inserting additional code into the HTML.

英文:

I have the following HTML:

&lt;div class=&quot;row&quot;&gt;
 &lt;div class=&quot;col-lg-3 col-md-4 col-sm-6 col-xs-12 text-center wow fadeIn&quot;&gt;
&lt;a href=&quot;assets/img/our_works/big_img/1.png&quot;&gt;
&lt;img class=&quot;preview&quot; src=&quot;assets/img/our_works/1.png&quot; alt=&quot;window&quot;&gt;&lt;/a&gt;
&lt;/div&gt;
 &lt;div class=&quot;col-lg-3 col-md-4 col-sm-6 col-xs-12 text-center wow fadeIn&quot; data-wow-delay=&quot;0.1s&quot;&gt;
&lt;a href=&quot;assets/img/our_works/big_img/2.png&quot;&gt;
&lt;img class=&quot;preview&quot; src=&quot;assets/img/our_works/2.png&quot; alt=&quot;window&quot;&gt;&lt;/a&gt;
&lt;/div&gt;
           
        &lt;/div&gt;

I need to write a JS script so that when a user clicks on an image modal window appears with a bigger image (big_img/1.png) with a half transparent background.

I wrote this code:
const previewImages = document.querySelectorAll(".preview");

previewImages.forEach((previewImage) =&gt; {
 
  previewImage.addEventListener(&quot;click&quot;, (event) =&gt; {
   
    event.preventDefault();
    
    const imagePath = event.target.src;
    const bigImagePath = imagePath.replace(&quot;our_works&quot;, &quot;our_works/big_img&quot;);

but then stuck how it is better to do without inserting additional code into HTML.

答案1

得分: 0

The approach would be to actually create the HTML element(s) for the modal in your JS-code and then append it to the document.

W3Schools has actually a good modal image tutorial, which you could easily alter, check it out: https://www.w3schools.com/howto/howto_css_modal_images.asp

What you want to change there is instead of getting the modal from the existing document, you want to create the modal elements within your JS code and append it to the document. But as a personal thought: I think it's much cleaner to have the modal already in HTML, to keep a proper separation of concerns.

英文:

The approach would be to actually create the HTML element(s) for the modal in your JS-code and then append it to the document.

W3Schools has actually a good modal image tutorial, which you could easily alter, check it out: https://www.w3schools.com/howto/howto_css_modal_images.asp

What you want to change there is instead of getting the modal from existing document, you want to create the modal elements within your JS code and append it to the document. But as personal thought: I think it's much cleaner to have the modal already in HTML, to keep a proper seperation of concerns.

huangapple
  • 本文由 发表于 2023年5月10日 23:42:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220355.html
匿名

发表评论

匿名网友

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

确定