2017-04-14 2 views
0

내 코드에 모달을 포함하려고합니다. 버튼/href를 클릭하면 다른 파일이 모달에 표시됩니다. 간단히 코드를 here에서 가져 왔습니다.모달은 대부분 즉시 종료됩니다.

이제 애니메이션을로드 한 후 (또는 몇 밀리 초 후) 모달을 닫습니다. 이상한 부분은 버튼을 몇 번 누르면 가끔 작동하고 파일이 표시된다는 것입니다.

// Get the modal 
 
var modal = document.getElementById('myModal'); 
 

 
// Get the button that opens the modal 
 
var btn = document.getElementById("myBtn"); 
 

 
// Get the <span> element that closes the modal 
 
var span = document.getElementsByClassName("close")[0]; 
 

 
// When the user clicks on the button, open the modal 
 
btn.onclick = function() { 
 
    modal.style.display = "flex"; 
 
}; 
 

 
// When the user clicks on <span> (x), close the modal 
 
span.onclick = function() { 
 
    modal.style.display = "none"; 
 
}; 
 

 
// When the user clicks anywhere outside of the modal, close it 
 
window.onclick = function(event) { 
 
    if (event.target == modal) { 
 
     modal.style.display = "none"; 
 
    } 
 
};
/* The Modal (background) */ 
 
.modal { 
 
    display: none; /* Hidden by default */ 
 
    position: fixed; /* Stay in place */ 
 
    z-index: 1; /* Sit on top */ 
 
    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/Box */ 
 
.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); 
 
} 
 

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

 
/* Modal Footer */ 
 
.modal-footer { 
 
    padding: 2px 16px; 
 
    background-color: #5cb85c; 
 
    color: white; 
 
} 
 

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

 
.close:hover, 
 
.close:focus { 
 
    color: black; 
 
    text-decoration: none; 
 
    cursor: pointer; 
 
}
<div class="nav1"> 
 

 
     <a href="index.php?page=users&action=login" id="myBtn">Login</a> 
 
     
 
</div> 
 

 
    <div id="myModal" class="modal"> 
 
     <div class="modal-content"> 
 
      <div class="modal-header"> 
 
       <span class="close">&times;</span> 
 
       <h2>Modal Header</h2> 
 
      </div> 
 
      <div class="modal-body"> 
 
       Modal Body 
 
    <?php 
 
    if (isset($_GET["page"])) { 
 
     switch ($_GET["page"]) { 
 
      case "post": 
 
       include "./system/post/index.php"; 
 
       break; 
 
      case "users": 
 
       include "./system/users/index.php"; 
 
       break; 
 
      default: 
 
       include "start.php"; 
 
       break; 
 
     } 
 
    } 
 
    else 
 
    { 
 
     include "start.php"; 
 
    } 
 
    ?> 
 

 
      </div> 
 
      <div class="modal-footer"> 
 
       Modal Footer 
 
      </div> 
 
     </div> 
 
     </div> 
 
    <script src="./system/style/modal.js"></script>

+0

그래서 문제를 조금이라도 녹음하면 he 도움이 될 것입니다. (내가 엿 같은 것을 알 수 있습니다 ^^) 그래서 3 번 빨리 버튼을 누르면 어떻게 든 알게되었습니다. https://drive.google. co.kr/file/d/0B4ilpgGKqcEZbzBZaHN3QlhLU1k /보기 – Niklas

답변

0

문제는 이벤트의 전파입니다 날 것으로 보인다 :

여기 내 코드 (관련 부분)입니다. btn 또는 span을 클릭 할 때마다 모달이 표시되지 않으며 결국 window.onclick이 발생하고 모달이 닫힙니다. 그래서 해결책은 btn/span 클릭 이벤트에 event.stopPropagation()을 추가하는 것입니다 btn.click 기능에

btn.onclick = function(event) { 
    event.stopPropagation(); // stop the event to bubble up 
    modal.style.display = "flex"; 
}; 
span.onclick = function(event) { 
    event.stopPropagation(); // stop the event to bubble up 
    modal.style.display = "none"; 
}; 
window.onclick = function(event) { 
    if (event.target == modal) { 
    modal.style.display = "none"; 
    } 
}; 
+0

여러분의 모든 노력에 감사드립니다. 파이어 폭스처럼 코드에서 아무 일도 일어나지 않는 것처럼 보입니다. 크롬에서는 똑같은 문제가 빠져 나갑니다. – Niklas

0
// Get the modal 
var modal = document.getElementById('myModal'); 

// Get the button that opens the modal 
var btn = document.getElementById("myBtn"); 

// Get the <span> element that closes the modal 
var span = document.getElementsByClassName("close")[0]; 

// When the user clicks on the button, open the modal 
btn.onclick = function() { 
    modal.style.display = "Block"; 
    $('a').click(function(e) {e.preventDefault(); /*your_code_here;*/ return false; }); 

}; 

// When the user clicks on <span> (x), close the modal 
span.onclick = function() { 
    modal.style.display = "none"; 
}; 

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

그냥 편집이 난 당신이 "여기에 코드"에 링크를 넣어위한 샘플 코드를 만들 수 있습니다.

+0

먼저 노력에 감사드립니다. 어떤 코드 (어떤 부분)를 */여기에 입력해야합니까 * /? 미안 나는 js와 결코 "일했다" – Niklas

+0

다만 당신의 연결을 내려 놓으십시오 – Hacker