简体   繁体   English

如何设置模态弹出多张图片

[英]How set Modal popup multiple image

I'm try to use this from w3s to show multi images in modal popup:我尝试使用w3s中的这个在模态弹出窗口中显示多张图像:

 // Get the modal var modal = document.getElementById('myModal'); // Get the image and insert it inside the modal - use its "alt" text as a caption 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; } // Get the <span> element that closes the modal var span = document.getElementsByClassName("close")[0]; // When the user clicks on <span> (x), close the modal span.onclick = function() { modal.style.display = "none"; }
 /* Style the Image Used to Trigger the Modal */ #myImg { border-radius: 5px; cursor: pointer; transition: 0.3s; } #myImg: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 (Image Text) - Same Width as the Image */ #caption { margin: auto; display: block; width: 80%; max-width: 700px; text-align: center; color: #ccc; padding: 10px 0; height: 150px; } /* Add Animation - Zoom in the Modal */.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%; } }
 <.-- Trigger the Modal --> <img id="myImg" src="img_fjords,jpg" alt="Trolltunga. Norway" width="300" height="200"> <.-- The Modal --> <div id="myModal" class="modal"> <.-- The Close Button --> <span class="close" onclick="document;getElementById('myModal').style.display='none'">&times;</span> <!-- Modal Content (The Image) --> <img class="modal-content" id="img01"> <!-- Modal Caption (Image Text) --> <div id="caption"></div> </div>

It work fine for one image.它适用于一张图片。

On my site I have some pages that I need to show more image, and if I set this code for other image, the popup work only for the first and not for other images.在我的网站上,我有一些页面需要显示更多图像,如果我将此代码设置为其他图像,则弹出窗口仅适用于第一张图像,不适用于其他图像。

How set correctly html and javascript?如何正确设置 html 和 javascript?

Best solution that I have found:我发现的最佳解决方案:

Fisrt, the HTML changes:首先,HTML 更改:

Ex: Each image needs their own ID... very minor change to your html... (changes from w3school.com from that original file.例如:每个图像都需要自己的 ID... 对您的 html 进行非常小的更改...(从原始文件的 w3school.com 更改。

<div class="image"><img **id="myImg2"** src="images/pic02.jpg" alt="" /></div>
<div class="image"><img **id="myImg3"** src="images/pic03.jpg" alt="" /></div>

Original原来的

    <div class="image"><img **id="myImg" src="images/pic01.jpg" alt="" /></div>

Second, CSS changes:二、CSS变化:

Copy the original "#myImg" and "#myImg:hover" attributes and paste right under .... adding the orinal new (number) after.复制原始的“#myImg”和“#myImg:hover”属性,然后粘贴到 .... 之后添加原始的新(数字)。 Each image needs their unique "myImg"and their unique "#myImg:hover"每个图像都需要其独特的“myImg”和独特的“#myImg:hover”

Ex:前任:

#myImg2 {
    border-radius: 5px;
    cursor: pointer;
    transition: 0.3s;
}

#myImg3 {
    border-radius: 5px;
    cursor: pointer;
    transition: 0.3s;
}

#myImg2:hover {opacity: 0.7;}

#myImg3:hover {opacity: 0.7;}

Original原来的

    #myImg {
        border-radius: 5px;
        cursor: pointer;
        transition: 0.3s;
    }

    #myImg:hover {opacity: 0.7;}

----

Javascript changes: Added the following: Add the var "variables" for each image and the " click " function for each image: --- Ex: JavaScript更改:添加了以下:添加VAR“变量”为每个图像和“点击”功能为每个图像:---例如:

    var img2 = document.getElementById('myImg2');
    var img3 = document.getElementById('myImg3');

    //handle click for myImg2
    img2.onclick = function(){
        modal.style.display = "block";
        modalImg.src = this.src;
        captionText.innerHTML = this.alt;
    }

    //handle click for myImg3
    img3.onclick = function(){
        modal.style.display = "block";
        modalImg.src = this.src;
        captionText.innerHTML = this.alt;
    }

Orginal原版

<script>
    // Get the modal
var modal = document.getElementById('myModal');

// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById('myImg');
var modalImg = document.getElementById("img01");
var img2 = document.getElementById('myImg2');
var img3 = document.getElementById('myImg3');
var captionText = document.getElementById("caption");
img.onclick = function(){
    modal.style.display = "block";
    modalImg.src = this.src;
    captionText.innerHTML = this.alt;
}

//handle click for myImg2
img2.onclick = function(){
    modal.style.display = "block";
    modalImg.src = this.src;
    captionText.innerHTML = this.alt;
}

//handle click for myImg3
img3.onclick = function(){
    modal.style.display = "block";
    modalImg.src = this.src;
    captionText.innerHTML = this.alt;
}

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on <span> (x), close the modal
span.onclick = function() { 
  modal.style.display = "none";
}
    </script>

Here is an example of something very easy to understand:这是一个很容易理解的例子:

<!DOCTYPE html>
<html>
<head>
<style>
#myImg {
    border-radius: 5px;
    cursor: pointer;
    transition: 0.3s;
}

#myImg2 {
    border-radius: 5px;
    cursor: pointer;
    transition: 0.3s;
}

#myImg3 {
    border-radius: 5px;
    cursor: pointer;
    transition: 0.3s;
}

#myImg:hover {opacity: 0.7;}

#myImg2:hover {opacity: 0.7;}

#myImg3: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%;
    }
}
</style>
</head>
<body>

<h2>Image Modal</h2>
<p>Guitarfetish.com </p>
<p>Gallery ideas</p>

<img id="myImg" src="img01.jpg" alt="XGP Tele Bodies" width="600" height="200">

<!-- The Modal -->
<div id="myModal" class="modal">
  <span class="close">&times;</span>
  <img class="modal-content" id="img01">
  <div id="caption"></div>
</div>

<img id="myImg2" src="img02.jpg" alt="Xaviere Guitars" width="600" height="200">

<img id="myImg3" src="img03.jpg" alt="Premium Guitar Cases" width="600" height="200">
<script>
// Get the modal
var modal = document.getElementById('myModal');

// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById('myImg');
var img2 = document.getElementById('myImg2');
var img3 = document.getElementById('myImg3');
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;
}

//handle click for myImg2
img2.onclick = function(){
    modal.style.display = "block";
    modalImg.src = this.src;
    captionText.innerHTML = this.alt;
}

//handle click for myImg3
img3.onclick = function(){
    modal.style.display = "block";
    modalImg.src = this.src;
    captionText.innerHTML = this.alt;
}

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on <span> (x), close the modal
span.onclick = function() { 
    modal.style.display = "none";
}
</script>

</body>
</html>

This implementation will not work as you are referring to the same DOM element by ID.当您通过 ID 引用相同的 DOM 元素时,此实现将不起作用。 You can try this JSFiddle which uses magnific popup plugin to make a multiple image modal.你可以试试这个 JSFiddle,它使用magnific popup plugin来制作多图像模态。 This would be easier for you to implement as well.这对您来说也更容易实施。

Here is an example of magnific popup: https://jsfiddle.net/8f60f93t/2/这是一个放大的弹出窗口示例: https : //jsfiddle.net/8f60f93t/2/

How to do this for case that you do not know how many images will be needed...?如果您不知道需要多少张图片,该怎么做...? For example I have a CSS popup that works for my needs well, but works only for one id.例如,我有一个 CSS 弹出窗口可以很好地满足我的需求,但仅适用于一个 id。

Looks like this:看起来像这样:

            <div class="imagelist">
          <{if $product.img_url=="" && $otherimg_num==0}>
            <div class="u-oneimage">
              <img src="https://img.shop-pro.jp/s_tmpl_img/28/noimage.png">
            </div>
            <{else}>
              <{if $product.img_url !="" }>
                <div class="<{if $otherimg_num == 0}>u-oneimage<{/if}>">
                  <a href="#test">
                    <img src="<{$product.img_url}>">
                  </a>
                </div>
                <{else}>
                  <div class="<{if $otherimg_num == 0}>u-oneimage<{/if}>">
                    <img src="https://img.shop-pro.jp/s_tmpl_img/28/noimage.png">
                  </div>
                  <{ /if}>
                    <{section name=num loop=$otherimg}>
                      <{if $otherimg[num].url !="" }>
                        <div>
                          <img src="<{$otherimg[num].url}>">
                        </div>
                        <{ /if}>
                          <{ /section}>
                            <{ /if}>
        </div>

        <a href="#" class="lightbox" id="test">
          <span><img src="<{$product.img_url}>"></span>
        </a>

How this case would be sorted then...?那么这个案子将如何处理……?

Thank you to everyone, who may help.谢谢大家,谁可以帮忙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM