最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Close pop up div by clicking outside of it - Stack Overflow

programmeradmin1浏览0评论

I am a total beginner in programming. I have been searching for an answer but none of the ones I have found seem to solve my problem. I used the how do I center javascript css popup div, no matter what the screen resolution? pop up div method explained in the question.

Is it possible to close a div by clicking outside of it with small changes to the following code?

<script type="text/javascript">
    function showPopUp(el) {
        var cvr = document.getElementById("cover")
        var dlg = document.getElementById(el)
        cvr.style.display = "block"
        dlg.style.display = "block"
        if (document.body.style.overflow = "scroll") {
            cvr.style.width = "1024"
            cvr.style.height = "100%"
        }
    }
    function closePopUp(el) {
        var cvr = document.getElementById("cover")
        var dlg = document.getElementById(el)
        cvr.style.display = "none"
        dlg.style.display = "none"
        document.body.style.overflowY = "scroll"
    }
</script>
<style type="text/css">
   #cover {
        display:        none;
        position:       fixed;
        left:           0px;
        top:            0px;
        width:          100%;
        height:         100%;
        background:     gray;
        filter:         alpha(Opacity = 50);
        opacity:        0.5;
        -moz-opacity:   0.5;
        -khtml-opacity: 0.5
    }

</style>

In HTML I have the number of divs hidden with the ids dialog1, dialog2 and so on. When a link is clicked, the div shows up and to close it I use an img link:

< a class="close" href="#2" onclick="closePopUp('dialog2');">< img src="/img/close.png" height="30px"></a >

I am a total beginner in programming. I have been searching for an answer but none of the ones I have found seem to solve my problem. I used the how do I center javascript css popup div, no matter what the screen resolution? pop up div method explained in the question.

Is it possible to close a div by clicking outside of it with small changes to the following code?

<script type="text/javascript">
    function showPopUp(el) {
        var cvr = document.getElementById("cover")
        var dlg = document.getElementById(el)
        cvr.style.display = "block"
        dlg.style.display = "block"
        if (document.body.style.overflow = "scroll") {
            cvr.style.width = "1024"
            cvr.style.height = "100%"
        }
    }
    function closePopUp(el) {
        var cvr = document.getElementById("cover")
        var dlg = document.getElementById(el)
        cvr.style.display = "none"
        dlg.style.display = "none"
        document.body.style.overflowY = "scroll"
    }
</script>
<style type="text/css">
   #cover {
        display:        none;
        position:       fixed;
        left:           0px;
        top:            0px;
        width:          100%;
        height:         100%;
        background:     gray;
        filter:         alpha(Opacity = 50);
        opacity:        0.5;
        -moz-opacity:   0.5;
        -khtml-opacity: 0.5
    }

</style>

In HTML I have the number of divs hidden with the ids dialog1, dialog2 and so on. When a link is clicked, the div shows up and to close it I use an img link:

< a class="close" href="#2" onclick="closePopUp('dialog2');">< img src="/img/close.png" height="30px"></a >
Share Improve this question edited Jan 27, 2019 at 11:28 pnuts 59.5k11 gold badges91 silver badges141 bronze badges asked May 31, 2013 at 9:48 KristineKristine 7472 gold badges9 silver badges22 bronze badges 4
  • 1 post your code to get the answer – Kingk Commented May 31, 2013 at 9:50
  • The code is in the link I posted! – Kristine Commented May 31, 2013 at 9:52
  • As a starter you would need a way of registering a mouseclick on the document and then closing the popup from there. Without your code thats as far as I can go – Mark Walters Commented May 31, 2013 at 9:53
  • I am using the same code that is in the link! But I will post it here! Thank you! – Kristine Commented May 31, 2013 at 9:53
Add a comment  | 

4 Answers 4

Reset to default 9

When you open the pop up, create an invisible div of height width 100%, which lies at the back of your pop-up div.

Attach an onclick function to the div:

document.getElementById('invisibleDiv').onclick = function()
{
    document.getElementById('popup').style.display = 'none'; 
}

Hope that helps

var $popup = $('#your-popup');

$('body').on('click', function(ev) {
   $popup.hide(); // click anywhere to hide the popup; all click events will bubble up to the body if not prevented
});

$popup.on('click', function(ev) {
   ev.stopPropagation(); // prevent event from bubbling up to the body and closing the popup
});

Without creating an invisible div just with javascript:

add this:

function getOffset(el) {
    var _x = 0;
    var _y = 0;
    while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {
        _x += el.offsetLeft - el.scrollLeft;
        _y += el.offsetTop - el.scrollTop;
        el = el.offsetParent;
    }
    return { top: _y, left: _x };
}

// function to check intersection rectangle <-> point
function intersects(ax1, ay1, ax2, ay2, px, py) {
    return !(ax2 < px || ax1 > px || ay2 < py || ay1 > py);
}

document.onclick = function(event) {
    var dialog = document.getElementById('dialog');
    var offset = getOffset(dialog);
    var ax1 = offset.left;
    var ay1 = offset.top;
    var ax2 = ax1 + 300 /* dialog width */;
    var ay2 = ay1 + 300 /* dialog height */;

    if(!intersects(ax1, ay1, ax2, ay2, event.pageX, event.pageY)) {
        closePopUp('dialog');
    }
};

Yes... Important you remove the overlay and that is <div class="ui-widget-overlay ui-front"></div> when use the Jquery 2.0.0.js... (It may be that with the previous libraries change the name)

Example...

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.0.0.js"></script>

    <script type="text/javascript" src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<link href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
</head>
    <script type="text/javascript">
// <---- VENTAÑAS DE PARAMETERES---->
$(document).ready(function() { 
$( "#wnd_Paramedit" ).dialog({
                autoOpen: false,
                height: 'auto',
                width: 405,
                modal: true,
                resizable:false,
                buttons: {
                    "Accept": function() {


                        $( this ).dialog( "close" );    
                    },
                    Cancel: function() {
                        $( this ).dialog( "close" );
                    }
                },
                close: function() {
                    $( this ).dialog( "close" );
                }
            });

            $( "#btn_Pedit" ).click(function() {
                    $( "#wnd_Paramedit" ).dialog( "open" );
$("div").removeClass("ui-widget-overlay ui-front");
                });
});
</script>
<body>
<h3>List of parameters</h3>
<div id="sortparam" >


</div>
<input type="button" id="btn_Pedit" value="Edit"/>
<div id="wnd_EditParam" title="Edit Parameter"></div>
</body>
<div id="wnd_Paramedit" title="Choose parameter" >
    <label> inserta el nombre del Parameter que quiere modificar<label><br />
    <label> ID <input type=text size=30 id="med"></label>      
</div>
</html>

Now you can create the other particular button for close the window pop up and you paste this specific code for close:

$( "#wnd_Paramedit" ).dialog("close");

or

$( "#wnd_Paramedit" ).dialog("destroy");
发布评论

评论列表(0)

  1. 暂无评论