javascript - 在页面刷新/重新访问时关闭覆盖

标签 javascript html css

我有以下 HTML、CSS 和 Javascript:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script type="text/javascript">

function getTarget(event){
    target = (typeof window.event === "undefined")?event.target:window.event.srcElement;
    return target;
}

document.onclick = function(event){
    var target = getTarget(event);
    if(target.id == ""){
        var overlay = document.getElementById('overlay');
        var specialBox = document.getElementById('specialBox');
        var button = document.getElementById('qualityButton');
        overlay.style.display = "none";
        specialBox.style.display = "none";
        button.style.color="#000000";
    }

}


function toggleOverlay(){
    var overlay = document.getElementById('overlay');
    var specialBox = document.getElementById('specialBox');
    var button = document.getElementById('qualityButton');
    overlay.style.opacity = .7;
    if(overlay.style.display == "block"){
        overlay.style.display = "none";
        specialBox.style.display = "none";
        button.style.color="#000000";
    } else {
        overlay.style.display = "block";
        specialBox.style.display = "block";
        button.style.color="#ff0000";
    }
}
</script>

<style type="text/css">

div#overlay {
    display: none;
    z-index: 2;
    background: #A9A9A9;
    position: fixed;
    width: 879px;
    height: 291px;
    top: 50px;
    left: 0px;
    text-align: center;
}
div#specialBox {
    display: none;
    position: fixed;
    z-index: 3;
    width: 719px; 
    height: 215px;
    top: 88px;
    left: 80px;
    background: #FFF;
}
div#wrapper {
    position:absolute;
    top: 0px;
    left: 0px;
    padding-left: 24px;
}
</style>

<style type="text/css">
    .btn {
        cursor:pointer;
        font-size:24px;
        border:none;
        color:#000
}
    .btn:hover{ 
    color:#F00; 
    }
</style>

<style type="text/css">
    .x {
        background-color:white;
        cursor:pointer;
        font:Arial;
        font-size:14px;
        color:red;
        z-index: 4;
        position: fixed;
        top: 92px;
        left: 766px;
    }
</style>

</head>

<body>
<!-- Start Overlay -->
<div id="overlay"></div>
<!-- End Overlay -->
<!-- Start Special Centered Box -->
<div id="specialBox">
  <button class="x" onmousedown="toggleOverlay()">X</button>
</div>
<!-- Start Special Centered Box -->
<!-- Start Normal Page Content -->
<div id="wrapper">
  <button id="qualityButton" class="btn" onmousedown="toggleOverlay()">HIGHEST QUALITY</button>
</div>
<!-- End Normal Page Content -->



</div>
</body>
</html>

叠加层打开和关闭都很好。但是,如果打开叠加层并且用户访问网站上的另一个页面,然后返回到叠加层所在的原始页面,则叠加层仍保持打开状态......有没有办法在刷新页面时关闭叠加层并返回访问过另一个页面后?

非常感谢任何帮助:)

詹姆斯

最佳答案

尝试对要在刷新时隐藏的 div 使用 display none

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    <script type="text/javascript">

        function getTarget(event){
            target = (typeof window.event === "undefined")?event.target:window.event.srcElement;
            return target;
        }

        document.onclick = function(event){
            var target = getTarget(event);
            if(target.id == ""){
                var overlay = document.getElementById('overlay');
                var specialBox = document.getElementById('specialBox');
                var button = document.getElementById('qualityButton');
                overlay.style.display = "none";
                specialBox.style.display = "none";
                button.style.color="#000000";
            }

        }


        function toggleOverlay(){
            var overlay = document.getElementById('overlay');
            var specialBox = document.getElementById('specialBox');
            var button = document.getElementById('qualityButton');
            overlay.style.opacity = .7;
            if(overlay.style.display == "block"){
                overlay.style.display = "none";
                specialBox.style.display = "none";
                button.style.color="#000000";
            } else {
                overlay.style.display = "block";
                specialBox.style.display = "block";
                button.style.color="#ff0000";
            }
        }
    </script>

    <style type="text/css">

        div#overlay {
            display: none;
            z-index: 2;
            background: #A9A9A9;
            position: fixed;
            width: 879px;
            height: 291px;
            top: 50px;
            left: 0px;
            text-align: center;
        }
        div#specialBox {
            display: none;
            position: fixed;
            z-index: 3;
            width: 719px;
            height: 215px;
            top: 88px;
            left: 80px;
            background: #FFF;
        }
        div#wrapper {
            position:absolute;
            top: 0px;
            left: 0px;
            padding-left: 24px;
        }
    </style>

    <style type="text/css">
        .btn {
            cursor:pointer;
            font-size:24px;
            border:none;
            color:#000
        }
        .btn:hover{
            color:#F00;
        }
    </style>

    <style type="text/css">
        .x {
            background-color:white;
            cursor:pointer;
            font:Arial;
            font-size:14px;
            color:red;
            z-index: 4;
            position: fixed;
            top: 92px;
            left: 766px;
        }
    </style>

</head>

<body>
<!-- Start Overlay -->
<div id="overlay" style="display: none"></div>
<!-- End Overlay -->
<!-- Start Special Centered Box -->
<div id="specialBox">
    <button class="x" onmousedown="toggleOverlay()">X</button>
</div>
<!-- Start Special Centered Box -->
<!-- Start Normal Page Content -->
<div id="wrapper">
    <button id="qualityButton" class="btn" onmousedown="toggleOverlay()">HIGHEST QUALITY</button>
</div>
<!-- End Normal Page Content -->



</div>
</body>
</html>

关于javascript - 在页面刷新/重新访问时关闭覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23883467/

相关文章:

javascript - ajax页面加载内容后加载jquery插件js

html - 如何使用 CSS 垂直对齐图像旁边的多个文本?

css - 使用@font-face 定义小型大写字体变体

css - 减轻另一个类(class)的背景颜色

javascript - 为 ng-model angular.js 提交表单 ng-repeat

javascript - 如何将类添加到 TinyMCE 表编辑器的下拉菜单中

php - 使用javascript两次创建缓存图像

javascript - 添加动画以填充进度条

javascript - 如果 hasClass 帮助和从变量中减去

css - 使用::before 创建元素符号后缩进整个列表项