2016-09-01 2 views
0

実際にポップアップが何度も繰り返されます。ユーザーがマウスを画面の外に置くたびにJsのリミッタが一度にポップアップを伴うアクション

私のポップアップは一度しか表示されません。ユーザが閉じると、ポップアップが戻らない場合があります。

私はページを再読み込みする必要があります。 close divも機能しません。

すぐにhtmlファイルでテストできます。 onclickハンドラの

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta charset="UTF-8"> 
    <title>Title</title> 
    <style type="text/css"> 
     .modal { 
     display: none; /* Hidden by default */ 
     position: fixed; /* Stay in place */ 
     z-index: 1; /* Sit on top */ 
     padding-top: 100px; /* Location of the box */ 
     left: 0; 
     top: 0; 
     width: 100%; /* Full width */ 
     height: 100%; /* Full height */ 
     overflow: auto; /* Enable scroll if needed */ 
     background-color: rgb(0,0,0); /* Fallback color */ 
     background-color: rgba(0,0,0,0.4); /* Black w/ opacity */ 
     } 

     /* Modal Content */ 
     .modal-content { 
     position: relative; 
     background-color: #fefefe; 
     margin: auto; 
     padding: 0; 
     border: 1px solid #888; 
     width: 80%; 
     box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19); 
     -webkit-animation-name: animatetop; 
     -webkit-animation-duration: 0.4s; 
     animation-name: animatetop; 
     animation-duration: 0.4s 
     } 

     /* Add Animation */ 
     @-webkit-keyframes animatetop { 
     from {top:-300px; opacity:0} 
     to {top:0; opacity:1} 
     } 

     @keyframes animatetop { 
     from {top:-300px; opacity:0} 
     to {top:0; opacity:1} 
     } 

     /* The Close Button */ 
     .close { 
     color: white; 
     float: right; 
     font-size: 28px; 
     font-weight: bold; 
     } 

     .close:hover, 
     .close:focus { 
     color: #000; 
     text-decoration: none; 
     cursor: pointer; 
     } 

     .modal-header { 
     padding: 2px 16px; 
     background-color: #5cb85c; 
     color: white; 
     } 

     .modal-body {padding: 2px 16px;} 

     .modal-footer { 
     padding: 2px 16px; 
     background-color: #5cb85c; 
     color: white; 
     } 
    </style> 
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> 

    </head> 
    <body> 

    <h1> JE SUIS UN LAPIN NINJA!!!!</h1> 


    <div> coucou les enfants</div> 


    <div id="myModal" class="modal"> 

     <!-- Modal content --> 
     <div class="modal-content"> 
     <div class="close">X</div> 
     <div class="modal-header"> 
      <h2 class="center-pop-up">Laissez nous une note.</h2> 
     </div> 
     <div class="modal-body"> 
      <form method="post" id="add_comment"> 
      <input type="text" value="" name="page"> 
      <input type="text" value="" name="page" id="id_comment"> 
      </form> 

     </div> 
     <div class="modal-footer"> 
      <h3 class="center-pop-up">Merci de votre passage sur notre site!</h3> 
     </div> 
     </div> 

    </div> 
    <script type="text/javascript"> 
     // Get the modal 
     var modal = document.getElementById('myModal'); 

     // Get the <span> element that closes the modal 
     var body = document.body 
     var close = document.getElementsByClassName("close"); 
     var oneuponceatime = true; 
     var comment = document.getElementById("id_comment"); 

     comment.style.display = "none"; 

     close.onclick = function() { 
     modal.style.display = "none"; 
     } 

     console.log('body', body); 
     console.log('close', close); 


     if (oneuponceatime) { 
     body.onmouseleave = function(){ 
      modal.style.display = "block"; 
     } 
     oneuponceatime = false; 
     console.log(oneuponceatime); 
     } 
     else{ 
     console.log("coucou"); 
     } 



     // When the user clicks anywhere outside of the modal, close it 
     window.onclick = function(event) { 
     if (event.target == modal) { 
      modal.style.display = "none"; 
     } 
     } 
    </script> 


    <h1> JE SUIS UN LAPIN NINJA!!!!</h1> 


    <div> coucou les enfants</div> 
    <h1> JE SUIS UN LAPIN NINJA!!!!</h1> 


    <div> coucou les enfants</div> 
    <h1> JE SUIS UN LAPIN NINJA!!!!</h1> 


    <div> coucou les enfants</div> 
    <h1> JE SUIS UN LAPIN NINJA!!!!</h1> 


    <div> coucou les enfants</div> 
    <h1> JE SUIS UN LAPIN NINJA!!!!</h1> 


    <div> coucou les enfants</div> 
    <h1> JE SUIS UN LAPIN NINJA!!!!</h1> 


    <div> coucou les enfants</div> 
    <h1> JE SUIS UN LAPIN NINJA!!!!</h1> 


    <div> coucou les enfants</div> 
    <h1> JE SUIS UN LAPIN NINJA!!!!</h1> 


    <div> coucou les enfants</div> 
    <h1> JE SUIS UN LAPIN NINJA!!!!</h1> 


    <div> coucou les enfants</div> 
    <h1> JE SUIS UN LAPIN NINJA!!!!</h1> 

    </body> 
</html> 

答えて

0

、あなたが再び登場モーダルの配列

var close = document.getElementsByClassName("close")[0]; 

の要素を逆参照する必要が、あなたはそれをトリガーしないようにしたい時はいつでもあなたのハンドラbody.onmouseleaveの設定を解除する必要があります。このように:

function closeModal() { 
    modal.style.display = "none"; 
    body.onmouseleave = null; 
} 

close.onclick = closeModal; 

// ... 
// When the user clicks anywhere outside of the modal, close it 
window.onclick = function(event) { 
    if (event.target == modal) { 
     closeModal(); 
    } 
} 

それを試してみてください:https://jsfiddle.net/bz5b0z04/1/

関連する問題