关闭模态窗口

时间:2014-02-24 22:53:32

标签: javascript jquery modal-window

我正在使用模态窗口调用登录(在Worpress中,但登录其他服务)。我正在尝试添加一个按钮来关闭模态窗口 - 它看起来应该可以工作,但它不是......任何想法?

这是我正在使用的js:

<script type="text/javascript">
function overlay() {
    el = document.getElementById("overlay");
    el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible";
}
$(function(){// document.ready shorthand
    $('.close-btn').click(function() {
        $('#overlay,.login-popup').fadeOut('3000',function(){//use 3000 in place of 300m
            $('#overlay').remove();
        });    
        return false;
    });
});

</script>

关闭按钮的html:

<a class="close-btn" href="#">Close</a>

和css:

#overlay {
 visibility: hidden;
 position: absolute;
 left: 0px;
 top: 0px;
 width:100%;
 height:100%;
 text-align:center;
 z-index: 1000;
 background-image:url(../background_trans.png);
 background-repeat:repeat;
}

#overlay_content {
 width:275px;
 width:19.64285714rem;
 margin: 100px auto;
 background-color: #ebf1e3;
 border:1px solid #ccc;
 padding:25px;
 text-align:left;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
line-height:175%;
}

1 个答案:

答案 0 :(得分:0)

不要remove()覆盖元素,只需用visibility:hidden隐藏它,并将.html()设置为null,但只有当你想要删除模态窗口中当前的内容时才会显示。

来源:我最近做了类似的事情(阅读elements.js源代码并绕过网格来启动模态弹出窗口)(http://fooscript.com/zombie/

以下是我用来清除模态的代码

clearModal : function() {
    this.modal.innerHTML = null;
    this.triggerOverlay();
},
//when called, triggers the visibility of the overlay that disables the page
//modal window
triggerOverlay : function() {
    this.overlay.style.visibility = (this.overlay.style.visibility == "visible") ? "hidden" : "visible";
},