如何创建模态图像网格?

时间:2017-02-11 21:09:03

标签: javascript html css css3

我正在创建一个具有图像网格的网站。我希望每个图像在单击时扩展为模态。我已经用它来处理一个图像,但我必须为每个图像创建一个新的模态,新的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>

1 个答案:

答案 0 :(得分:0)

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

<code>

所以这将是动态的。