繁体   English   中英

如何创建模态图像网格?

[英]How to create a grid of modals images?

我正在创建一个以图像网格为特色的网站。 我希望每个图像在单击时都展开为模态。 我已经将其与一个图像一起使用,但是我必须为每个图像创建一个新的模式,新的myImg和新的脚本。 无论如何,我只能拥有一个模态和一个myImg并编写脚本以使其通常可用于每个图像,这样我就不必为每个图像创建新的模态和myImg,因为那样很快就会变得很长。

网格示例:

源代码:

<!DOCTYPE html>

<html>
    <?php
        $myfile = fopen("header.html", "r") or die("Unable to open file!");
        echo fread($myfile,filesize("header.html"));
        fclose($myfile);
    ?>
    <style>
        .img {
            font-size: 0;
        }

        a1 {
            font-size: 16px; 
            display: inline-block;
            margin-bottom: 8px;
            width: calc(50% - 4px);
            margin-right: 8px;
        }

        a1:nth-of-type(2n) {
            margin-right: 0;
        }

        @media screen and (min-width: 50em) {
            a1 {
                width: calc(25% - 6px);
            }

            a1:nth-of-type(2n) {
                margin-right: 8px;
            }

            a1:nth-of-type(4n) {
                margin-right: 0;
            }
        }

        img {
            border: none;
            max-width: 100%;
            height: auto;
            display: block;
            background: #ccc;
            transition: transform .2s ease-in-out;
        }

        figure {
            margin: 0;
            overflow: hidden;
        }

        /* 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;
        }

        /* 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%;
            }
        }


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

        #myImg:hover {opacity: 0.7;}

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

        #myImg2:hover {opacity: 0.7;}

    </style>
    <body>

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

        <div id="myModal2" class="modal">
            <span class="close">&times;</span>
            <img class="modal-content" id="img02">
            <div id="caption"></div>
        </div>

        <div class="img">
            <a1 href="abe_lincoln_riding_a_grizzly.png">
                <figure>
                    <img id="myImg" src="abe_lincoln_riding_a_grizzly.png" alt="">
                </figure>
            </a1>

            <a1 href="abe_lincoln_riding_a_grizzly.png">
                <figure>
                    <img id="myImg2" src="abe_lincoln_riding_a_grizzly.png" alt="">
                </figure>
            </a1>

            <a1 href="abe_lincoln_riding_a_grizzly.png">
                <figure>
                    <img src="abe_lincoln_riding_a_grizzly.png" alt="">
                </figure>
            </a1>

            <a1 href="abe_lincoln_riding_a_grizzly.png">
                <figure>
                    <img src="abe_lincoln_riding_a_grizzly.png" alt="">
                </figure>
            </a1>

            <a1 href="abe_lincoln_riding_a_grizzly.png">
                <figure>
                    <img src="abe_lincoln_riding_a_grizzly.png" alt="">
                </figure>
            </a1>

            <a1 href="abe_lincoln_riding_a_grizzly.png">
                <figure>
                    <img src="abe_lincoln_riding_a_grizzly.png" alt="">
                </figure>
            </a1>

            <a1 href="abe_lincoln_riding_a_grizzly.png">
                <figure>
                    <img src="abe_lincoln_riding_a_grizzly.png" alt="">
                </figure>
            </a1>

            <a1 href="abe_lincoln_riding_a_grizzly.png">
                <figure>
                    <img src="abe_lincoln_riding_a_grizzly.png" alt="">
                </figure>
            </a1>

            <a1 href="abe_lincoln_riding_a_grizzly.png">
                <figure>
                    <img src="abe_lincoln_riding_a_grizzly.png" alt="">
                </figure>
            </a1>
        </div>

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

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

        <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 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";
            }
        </script>
    </body>

</html>

使用Jquery来获取单击的img的ID和信息,并使函数使用id和info参数创建模态,然后将单击的img的ID和信息传递给函数,然后将其添加到vars ex:

var modalImg = document.getElementById("img" + imgIdNumber);
etc...

因此它将是动态的。

暂无
暂无

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

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