关闭弹出框,鼠标点击外边界

时间:2015-02-03 16:40:22

标签: javascript html

我已经制作了这个主页。这个弹出框有登录字段。当弹出窗口时,背景变暗。但是如果用户点击框外边界的任何位置,弹出窗口应该关闭。我创建了函数closeOverlay,当用户点击" esc"在键盘上,但当用户点击框外的边界时没有任何反应。如何在两种情况下都可以使用它,当用户遇到逃生时,或者将鼠标移到框边界并点击

这是HTML代码

<head>
        <link rel="stylesheet" type="text/css" href="LAb2css.css">
</head>
<script>
    window.onload = function() {


        setInterval(move, 10000);

    }



    function move() { //makes the box move in relation to the box div
              var d = document.getElementById('moveQuestion');
              var boxh = document.getElementById('boxDIV').clientHeight - 100; //keeps the image in the box by subtracting height
              var boxw = document.getElementById('boxDIV').clientWidth - 100; //keeps the image in the box by subtracting the width

              document.onmousemove = function calc(e) {
                var x = e.clientX ;
                var y = e.clientY;

                if (x < boxw) {
                    d.style.left = x +'px';
                }

                if (y < boxh) {
                    d.style.top = y +'px';
                }

              };
          };


function signin() { //displays the sign in form
            var lg = document.getElementById('login');
            var ov = document.getElementById('BackDIV');
            ov.style.display = 'block';
            lg.style.display = 'block';
          };

function join() { //calls the join form for users to register

            var lg = document.getElementById('signup');
            var ov = document.getElementById('BackDIV');
            ov.style.display = 'block';
            lg.style.display = 'block';
            activeForm = 1; //sets the active form counter to 1; a form is active
        };
function recoverPass() { //displays the recoverPassword form for the user to recover password

            var ov = document.getElementById('BackDIV');
            ov.style.display = 'block';

            var rp = document.getElementById('recoverPassword');
            rp.style.display = 'block';
             //sets the active form counter to 1; a form is active
        };    





         function closeOverlay() { // function closes the black transparent overlay div by setting the display properties of all the forms to none 
            var ov = document.getElementById('BackDIV');
            var rp = document.getElementById('recoverPassword');
            var lg = document.getElementById('login');
            var sg = document.getElementById('signup');

            ov.style.display = 'none';
            rp.style.display = 'none';
            lg.style.display = 'none';
            sg.style.display = 'none';
          };




     function closeOverlay() { // function closes the black transparent overlay div by setting the display properties of all the forms to none 
            var ov = document.getElementById('BackDIV');
            var rp = document.getElementById('recoverPassword');
            var lg = document.getElementById('login');
            var sg = document.getElementById('signup');

            ov.style.display = 'none';
            rp.style.display = 'none';
            lg.style.display = 'none';
            sg.style.display = 'none';
          };



             function closeOverlay() { // function closes the black transparent overlay div by setting the display properties of all the forms to none 
            var ov = document.getElementById('BackDIV');
            var rp = document.getElementById('recoverPassword');
            var lg = document.getElementById('login');
            var sg = document.getElementById('signup');

            ov.style.display = 'none';
            rp.style.display = 'none';
            lg.style.display = 'none';
            sg.style.display = 'none';
          };


          document.onkeydown = function(evt) { //makes the escape key to call the closeOverlay() function
            evt = evt || window.event;
            if (evt.keyCode == 27) {
                closeOverlay();
            }
          };



</script>

<body onload = move()>

    <div id="BackDIV">
    </div>

    <p>
        <?php
            // Just for displaying the display type for testing

            echo 'Display type = ' . $display_type . '<br><br>';

            if ($display_type == 'Start') {
                echo 'StartPage<br>';
            }
            else if ($display_type == 'SignIn') {
                echo "StartPage with 'SignIn' popup box<br>"; 
            }
            else if ($display_type == 'Join') {
                echo "StartPage with 'Join' popup box<br>"; 
            }
            else if ($display_type == 'ForgotPassword') {
                echo "StartPage with 'ForgotPassword' popup box<br>"; 
            }
            else {
                echo 'StartPage with no popup box<br>'; 
            }
        ?>
    </p>


    <div id = "main">

        <div id = "dateDIV"><?php $t = time(); echo (date("Y-m-d H:i:s", $t)); ?></div>

        <div id = "titleDIV">
          <h2>Thompson Rivers University - Question/Answers</h2>                    
        </div>

        <div id ="dopDownDIV">
            <ul>
              <li>
                <a href = "#"><img src="images/responsive_menu.png" title="MENU" alt="Menu" id="MenuIMG" height="50PX" width="50px" /></a>
                <ul>
                  <li><a href="#" onclick = "signin()">SIGN IN</a></li>

                    <li><a href="#" onclick="join()">JOIN</a></li>

                    <li><a href="#" onclick="recoverPass()">FORGOT PASSWORD</a></li>
                </ul>
              </li>
            </ul>       
        </div>

        <div id="boxDIV"> <a href ="#"><img src ="images/question-mark.png" id="moveQuestion" title= "Image moving In Box" alt="ImgInbox" height = "100px" width = "100px"</a></div>

        <div id= "BottomDIV">
            <a href = "http://cs.tru.ca" target="_blank"> About Us </a>
        </div>

    </div>

<!--Div for Signup form on click -->
 <div id="signup" onclick="closeOverlay()">
            <form action="main.html" method="get" autocomplete="on">
                <input type="text" name="username" autofocus><br /><p>Your email address</p>
                <input type="password" name="password"><br /><p>Password</p>
                <br /><br /><br />
                <input id="formButton" type="submit" value="Join">
                <a href="StartPage.html">
                    <input type="button" value="Cancel"/>
                </a>
            </form>
 </div>


<!--DIv for recoverPassword -->

<div id="recoverPassword" onclick="closeOverlay()">
            <!--<h1>Recover Password</h1> -->
            <form action="main.html" method="get" autocomplete="on">
                <input type="text" name="recoveremail" placeholder="The email address you registered with" autofocus><br /><p>Username (or email address)</p>

                <input id="formButton" type="submit">
                <a href="StartPage.html">
                    <input type="button" value="Cancel"/>
                </a>
            </form>
        </div>  

<!--Div for Login -->
    <div id="login" onclick="closeOverlay()">

            <form action="controller.php" method="post" autocomplete="on" >
                <input type='hidden' name='command' value='SignIn'>
                <input type="text" name="user" autofocus value="<?php echo $username ?>"> <?php echo $error_msg_username; ?> ><br />                            <p>Username (or email address)</p>
                <input type="password" name="pass" value="<?php echo $password ?>"> <?php echo $error_msg_password ?> ><br /><p>Password</p>
                <br/>
                <input type="submit" id="formButton" value="Sign In" >
                <a href="StartPage.html">
                    <input type="button" value="Cancel"/>
                </a>


            </form>
        </div>





</body>

</html>

2 个答案:

答案 0 :(得分:0)

试试这个

<div id="BackDIV" onclick="closeOverlay();"></div>

答案 1 :(得分:0)

像这样?向窗口添加一个事件监听器,如果需要,删除确认。

var pop=document.getElementById('over');window.addEventListener('click',function(){
  confirm('close popup')?
  pop.style.display="none":pop.style.display="block";
  
});
#over{min-width:80%;min-height:  100px;background-color:red;}
<div id="over">v</div>