Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何创建情态动词图像的网格?_Javascript_Html_Css - Fatal编程技术网

Javascript 如何创建情态动词图像的网格?

Javascript 如何创建情态动词图像的网格?,javascript,html,css,Javascript,Html,Css,我正在创建一个以网格图像为特色的网站。我希望每个图像在单击时展开为一个模式。我已经让它与一个图像一起工作,但我必须为每个图像创建一个新的模式、新的myImg和新的脚本。无论如何,我可以只使用一个模式和一个myImg,然后编写一个脚本,使其对每个图像都能正常工作,这样我就不必为每个图像创建一个新的模式和myImg,因为它很快就会变得非常长 网格示例: 源代码: <!DOCTYPE html> <html> <?php $myfile = fo

我正在创建一个以网格图像为特色的网站。我希望每个图像在单击时展开为一个模式。我已经让它与一个图像一起工作,但我必须为每个图像创建一个新的模式、新的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>

.img{
字号:0;
}
a1{
字体大小:16px;
显示:内联块;
边缘底部:8px;
宽度:计算(50%-4px);
右边距:8px;
}
a1:第n种类型(2n){
右边距:0;
}
@媒体屏幕和屏幕(最小宽度:50em){
a1{
宽度:钙(25%-6px);
}
a1:第n种类型(2n){
右边距:8px;
}
a1:第n种类型(4n){
右边距:0;
}
}
img{
边界:无;
最大宽度:100%;
高度:自动;
显示:块;
背景:#ccc;
转换:转换。2s轻松输入输出;
}
身材{
保证金:0;
溢出:隐藏;
}
/*模态(背景)*/
.莫代尔{
显示:无;/*默认情况下隐藏*/
位置:固定;/*保持原位*/
z指数:1;/*位于顶部*/
填充顶部:100px;/*框的位置*/
左:0;
排名:0;
宽度:100%;/*全宽*/
高度:100%;/*全高*/
溢出:自动;/*根据需要启用滚动*/
背景色:rgb(0,0,0);/*回退色*/
背景色:rgba(0,0,0,0.9);/*黑色w/不透明度*/
}
/*模态内容(图像)*/
.模态内容{
保证金:自动;
显示:块;
宽度:80%;
最大宽度:700px;
}
/*模态图像字幕*/
#标题{
保证金:自动;
显示:块;
宽度:80%;
最大宽度:700px;
文本对齐:居中;
颜色:#ccc;
填充:10px0;
高度:150像素;
}
/*关闭按钮*/
.结束{
位置:绝对位置;
顶部:15px;
右:35px;
颜色:#f1f1;
字体大小:40px;
字体大小:粗体;
过渡:0.3s;
}
.关闭:悬停,
.结束:聚焦{
颜色:#bbb;
文字装饰:无;
光标:指针;
}
/*较小屏幕上的100%图像宽度*/
@仅介质屏幕和(最大宽度:700px){
.模态内容{
宽度:100%;
}
}
#myImg{
边界半径:5px;
光标:指针;
过渡:0.3s;
}
#myImg:hover{opacity:0.7;}
#myImg2{
边界半径:5px;
光标:指针;
过渡:0.3s;
}
#myImg2:悬停{不透明度:0.7;}
&时代;
//获取模态
var modal=document.getElementById('myModal2');
//获取图像并将其插入模式-使用其“alt”文本作为标题
var img=document.getElementById('myImg2');
var modalImg=document.getElementById(“img02”);
var captionText=document.getElementById(“caption”);
img.onclick=函数(){
modal.style.display=“块”;
modalImg.src=this.src;
captionText.innerHTML=this.alt;
}
//获取关闭模态的元素
var span=document.getElementsByClassName(“关闭”)[0];
//当用户单击(x)时,关闭模式对话框
span.onclick=function(){
modal.style.display=“无”;
}
//获取模态
var modal=document.getElementById('myModal');
//获取图像并将其插入模式-使用其“alt”文本作为标题
var img=document.getElementById('myImg');
var modalImg=document.getElementById(“img01”);
var captionText=document.getElementById(“caption”);
img.onclick=函数(){
modal.style.display=“块”;
modalImg.src=this.src;
captionText.innerHTML=this.alt;
}
//获取关闭模态的元素
var span=document.getElementsByClassName(“关闭”)[0];
//当用户单击(x)时,关闭模式对话框
span.onclick=function(){
modal.style.display=“无”;
}

使用Jquery获取单击的img的id和信息,并使用函数创建带有id和信息参数的模态,并传递id和信息
var modalImg = document.getElementById("img" + imgIdNumber);
etc...