简体   繁体   English

如何通过单击页面来关闭覆盖

[英]how to close an overlay by clicking the page

I've javascript file like this : 我有这样的javascript文件:

var $overlay_wrapper;
var $overlay_panel;

function show_overlay() {
    if ( !$overlay_wrapper ) append_overlay();
    $overlay_wrapper.fadeIn(700);
    $overlay_panel.fadeIn(700);
}

function hide_overlay() {
    $overlay_wrapper.fadeOut(500);
    $overlay_panel.fadeOut(500);
}

function append_overlay() {
    $overlay_wrapper = $('<div id="overlay"></div>').appendTo( $('BODY') );
    $overlay_panel = $('<div id="overlay-panel"></div>').appendTo( $('BODY') );

    $overlay_panel.html( 
    '<p>This is the overlay content</p><a href="#" class="hide-overlay">X Close</a>' 
     );

    attach_overlay_events();
}

function attach_overlay_events() {
    $('A.hide-overlay', $overlay_panel).click( function(ev) {
        ev.preventDefault();
        hide_overlay();
    });
}

$(function() {
    $('A.show-overlay').click( function(ev) {
        ev.preventDefault();
        show_overlay();
    });
});

but the overlay just closed if I click the "X Close" link . 但是如果我点击“ X Close”链接,则叠加层刚刚关闭。 I want an overlay also closed if I click in the page that's transparant(outside the content). 如果我单击透明的页面(在内容之外),我还希望覆盖层也关闭。 how can I do about that? 我该怎么办? please for help and suggest. 请寻求帮助并提出建议。 thank you. 谢谢。

When you append the overlay to the body, attach a click event listener: 将叠加层附加到主体时,请附加一个click事件监听器:

$('#overlay').click(function(){
   $('.hide-overlay').trigger('click');
});

When this div is clicked it will trigger the "x close" click handler; 单击此div时,将触发“ x close”点击处理程序;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM