javascript - 在同一页面中弹出多个 div

标签 javascript jquery html css

在我的一个元素中,我需要在同一页面上弹出多个 div。这意味着当用户点击一个链接时,一些内容应该在弹出窗口中打开。将有许多这样的链接,它们带有自己的弹出窗口。由于对 javascript 知之甚少,我试图为它编写一个 javascript,但它只适用于一个弹出窗口。当我点击第二个、第三个……链接时,只有第一个弹出窗口打开,而不是打开第二个、第三个……弹出窗口。这是我的代码。请告知对其的修改。

<!DOCTYPE html>
<html > 
    <head> 
        <script>
        window.document.onkeydown = function (e)
        {
            if (!e)
            {
                e = event;
            }
            if (e.keyCode == 27)
            {
                lightbox_close();
            }
        }

        function lightbox_open()
        {
            window.scrollTo(0,0);
            document.getElementById('light').style.display='block';
            document.getElementById('fade').style.display='block';  
        }

        function lightbox_close()
        {
            document.getElementById('light').style.display='none';
            document.getElementById('fade').style.display='none';
        }
    </script>

    <style>
        #fade
        {
            display: none;
            position: fixed;
            top: 0%;
            left: 0%;
            width: 100%;
            height: 100%;
            background-color: #000;
            z-index:1001;
            -moz-opacity: 0.7;
            opacity:.70;
            filter: alpha(opacity=70);
        }
        #light
        {
            display: none;
            position: absolute;
            top: 50%;
            left: 50%;
            width: 300px;
            height: 200px;
            margin-left: -150px;
            margin-top: -100px;                 
            padding: 10px;
            border: 2px solid #FFF;
            background: #CCC;
            z-index:1002;
            overflow:visible;
        }
    </style> 
</head> 
<body> 
    <a href="#" onclick="lightbox_open();">Open 1</a>
    <div id="light">div 1</div>
    <div id="fade" onClick="lightbox_close();"></div> 

    <a href="#" onclick="lightbox_open();">Open 2</a>
    <div id="light">div 2</div>
    <div id="fade" onClick="lightbox_close();"></div> 

    <a href="#" onclick="lightbox_open();">Open 3</a>
    <div id="light">div 3</div>
    <div id="fade" onClick="lightbox_close();"></div> 
</body>
</html>

最佳答案

这是一种实现您想要的方法。我相信它可以得到改进,但这取决于你。

首先,ID 在整个页面中应该是唯一的。如果您想对元素进行分组,请改为给它们一个共享的

经过更改,您的 HTML 将如下所示:

<a href="#" class="lightbox" onclick="lightbox_open(this)">Open 1</a>
<div class="light">div 1</div>
<div class="fade" onClick="lightbox_close()"></div>

<a href="#" class="lightbox" onclick="lightbox_open(this)">Open 2</a>
<div class="light">div 2</div>
<div class="fade" onClick="lightbox_close()"></div>

<a href="#" class="lightbox" onclick="lightbox_open(this)">Open 3</a>
<div class="light">div 3</div>
<div class="fade" onClick="lightbox_close()"></div>

你的 CSS:

html, body {
    width: 100%;
    height: 100%;
}
.fade {
    display: none;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: #000;
    z-index:1001;
    -moz-opacity: 0.7;
    opacity:.70;
    filter: alpha(opacity=70);
}
.light {
    display: none;
    position: absolute;
    top: 50%;
    left: 50%;
    width: 300px;
    height: 200px;
    margin-left: -150px;
    margin-top: -100px;
    padding: 10px;
    border: 2px solid #FFF;
    background: #CCC;
    z-index:1002;
    overflow:visible;
}

还有你的Javascript:

window.document.onkeydown = function (e) {
    if (!e) {
        e = event;
    }

    if (e.keyCode == 27) {
        lightbox_close();
    }
}

// Note that the function is receiving the clicked element reference.
function lightbox_open(el) {        
    window.scrollTo(0,0);

    // All the anchors that have a class lightbox.
    var anchors = document.querySelectorAll('a.lightbox');
    // All the elements with class light.
    var light = document.querySelectorAll('.light');
    // All the elements with class fade.
    var fade = document.querySelectorAll('.fade');

    // Iterate over the anchors elements.
    for (var i = 0; i < anchors.length; i++) {
        // If the anchor matches the clicked one.
        if (anchors[i] == el) {
            // Look for the light and fade with the same index
            // and display them.
            light[i].style.display = 'block';
            fade[i].style.display = 'block';
        }
    }
}

function lightbox_close() {
    // All the elements with class light or fade.
    var els = document.querySelectorAll('.light, .fade');

    // Loop through the list.
    for (var i = 0; i < els.length; i++) {
        // Hide them.
        els[i].style.display = 'none';
    }
}

Demo

关于javascript - 在同一页面中弹出多个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26296700/

相关文章:

表单数据的 Javascript 计算返回 NaN

javascript - 完全填满浏览器窗口的多列图像

javascript - ReactJS 如何在字符串中呈现 HTML 对象跨度元素

php - htmlentities($password_string) 是必要的吗?

javascript - API 数据未显示在 EJS 文件中

javascript - 检查窗口可以滚动多远?

c# - 如何在asp.net中使用jquery实现文件上传进度条?

javascript - 具有滑动功能的导航中的 anchor 链接跳得太远

javascript - 粘性页脚 CSS 使 jQuery 小部件在滚动时从各自的位置移动

javascript - 尝试理解 JQuery/Ajax Get/POST 调用