2017-11-23 7 views
1

이 팝업을 닫기위한 방법을 찾으려면 그 곳을 클릭하여 실패했습니다. 지금은 직접 클릭하면 닫힙니다.이 팝업을 클릭하여 닫으십시오.

도움을 주셔서 감사합니다.

/* Popup container - can be anything you want */ 
    .popup { 
    position: relative; 
    display: inline-block; 
    cursor: pointer; 
    -webkit-user-select: none; 
    -moz-user-select: none; 
    -ms-user-select: none; 
    user-select: none; 
    } 

    /* The actual popup */ 
    .popup .popuptext { 
    visibility: hidden; 
    width: 160px; 
    background-color: #555; 
    color: #fff; 
    text-align: center; 
    border-radius: 6px; 
    padding: 8px 0; 
    position: absolute; 
    z-index: 1; 
    bottom: 125%; 
    left: 50%; 
    margin-left: -80px; 
    } 

    /* Popup arrow */ 
    .popup .popuptext::after { 
    content: ""; 
    position: absolute; 
    top: 100%; 
    left: 50%; 
    margin-left: -5px; 
    border-width: 5px; 
    border-style: solid; 
    border-color: #555 transparent transparent transparent; 
    } 

    /* Toggle this class - hide and show the popup */ 
    .popup .show { 
    visibility: visible; 
    -webkit-animation: fadeIn 1s; 
    animation: fadeIn 1s; 
    } 

HTML

<h2>Popup</h2> 
    <div class="popup" onclick="myFunction()">Click me to toggle the popup! 
    <span class="popuptext" id="myPopup">A Simple Popup!</span> 
    </div> 

자바 스크립트

<script> 
    // When the user clicks on div, open the popup 
    function myFunction() { 
    var popup = document.getElementById("myPopup"); 
    popup.classList.toggle("show"); 
    } 
    </script> 

CSS는 당신의 도움에 미리 감사드립니다. 나는 당신의 팝업을 가정

+0

Whihc 하나가 당신의 팝업입니다) 당신이 필요로하는 무엇이든. 말도 안 돼! – Gacci

답변

0

는 # 팝업 - 컨테이너 사업부 컨테이너를

document.querySelector(":not(#popup-container)").addEventListener(function(event){ 
    popup.classList.remove("show") 
}) 

UPDATE

function myFunction(e) { 
 
    var popup = document.getElementById("myPopup"); 
 
    e.stopImmediatePropagation() 
 
    if(!popup.classList.contains('show')){ 
 
     popup.classList.add("show"); 
 
     document.querySelectorAll("*").forEach(function(ele) { 
 
     ele.addEventListener('click', myFunction) 
 
    }) 
 
    } 
 
    else{ 
 
    popup.classList.remove("show"); 
 
    document.querySelectorAll("*").forEach(function(ele) { 
 
     ele.removeEventListener('click', myFunction) 
 
    }) 
 
    } 
 
}
.popup { 
 
    position: relative; 
 
    display: inline-block; 
 
    cursor: pointer; 
 
    -webkit-user-select: none; 
 
    -moz-user-select: none; 
 
    -ms-user-select: none; 
 
    user-select: none; 
 
} 
 

 

 
/* The actual popup */ 
 

 
.popup .popuptext { 
 
    visibility: hidden; 
 
    width: 160px; 
 
    background-color: #555; 
 
    color: #fff; 
 
    text-align: center; 
 
    border-radius: 6px; 
 
    padding: 8px 0; 
 
    position: absolute; 
 
    z-index: 1; 
 
    bottom: 125%; 
 
    left: 50%; 
 
    margin-left: -80px; 
 
} 
 

 

 
/* Popup arrow */ 
 

 
.popup .popuptext::after { 
 
    content: ""; 
 
    position: absolute; 
 
    top: 100%; 
 
    left: 50%; 
 
    margin-left: -5px; 
 
    border-width: 5px; 
 
    border-style: solid; 
 
    border-color: #555 transparent transparent transparent; 
 
} 
 

 

 
/* Toggle this class - hide and show the popup */ 
 

 
.popup .show { 
 
    visibility: visible; 
 
    -webkit-animation: fadeIn 1s; 
 
    animation: fadeIn 1s; 
 
}
<h2>Popup</h2> 
 
<div class="popup" onclick="myFunction(event)">Click me to toggle the popup! 
 
    <span class="popuptext" id="myPopup">A Simple Popup!</span> 
 
</div>

+0

저는 약간의 newb입니다. 정확하게 당신의 충고를 따를 수 있도록 당신의 스 니펫에 당신의 솔루션을 보여줄 수 있습니까? 감사 Kinji. R –

0

가장 쉬운은에 리스너를 추가하는 것입니다 문서가 표시되면 클릭이 팝업 아이에서 발생하지 않으면 토글 (또는

var popup = document.getElementById('popup'); 
 
document.addEventListener('click', function(e){ 
 
    var parent; 
 
    if(!(parent = e.target.closest('#popup'))){ 
 
    popup.classList.remove('show'); 
 
    } 
 
});
/* Popup container - can be anything you want */ 
 
    .popup { 
 
    position: relative; 
 
    display: inline-block; 
 
    cursor: pointer; 
 
    -webkit-user-select: none; 
 
    -moz-user-select: none; 
 
    -ms-user-select: none; 
 
    user-select: none; 
 
    
 
    visibility: hidden; 
 
    opacity: 0; 
 
    } 
 

 
    /* The actual popup */ 
 
    .popup .popuptext { 
 
    width: 160px; 
 
    background-color: #555; 
 
    color: #fff; 
 
    text-align: center; 
 
    border-radius: 6px; 
 
    padding: 8px 0; 
 
    position: absolute; 
 
    z-index: 1; 
 
    bottom: 125%; 
 
    left: 50%; 
 
    margin-left: -80px; 
 
    } 
 

 
    /* Popup arrow */ 
 
    .popup .popuptext::after { 
 
    content: ""; 
 
    position: absolute; 
 
    top: 100%; 
 
    left: 50%; 
 
    margin-left: -5px; 
 
    border-width: 5px; 
 
    border-style: solid; 
 
    border-color: #555 transparent transparent transparent; 
 
    } 
 

 
    /* Toggle this class - hide and show the popup */ 
 
    .popup.show { 
 
    opacity: 1; 
 
    visibility: visible; 
 
    -webkit-animation: fadeIn 1s; 
 
    animation: fadeIn 1s; 
 
    }
<h2>Popup</h2> 
 
    <div class="popup show" id="popup">Click me to toggle the popup! 
 
     <span class="popuptext" id="myPopup">A Simple Popup!</span> 
 
    </div>

+0

귀하의 의견을 기다리고있는 Gacci. 귀하의 솔루션은 페이지의 일부가 클릭되었을 때 팝업이 나타나게합니까? 한 가지 문제가 해결되었지만 다른 문제가 발생했다고 생각합니까? –

+0

이것은 방향을 알려주는 예일뿐입니다. 당신의 본보기는 목표가 부족합니다. 달성하고자하는 것에 대한 명확한 방향이 없습니다! 거기에서 당신이 원한다 무엇이든을하기 쉽습니다! 원하는 동작은 무엇입니까 – Gacci

+0

로드시 표시되도록 변경 한 다음 팝업 자체가 아닌 곳을 클릭하면 사라집니다! – Gacci