单击远离它关闭此弹出窗口

时间:2017-11-23 03:12:44

标签: javascript css popup

我一直尝试通过点击它来找到关闭此弹出窗口的方法,但未成功。截至目前,只有直接点击它才会关闭。

提前感谢您的帮助。

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>

Java脚本

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

CSS

    /* 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;
    }

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

我假设你的弹出窗口有一个带有#popup-container

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

<强>更新

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>

答案 1 :(得分:0)

最简单的方法是将监听器添加到文档中,然后如果点击不是来自任何弹出子项,那么切换(或做任何你需要的)

&#13;
&#13;
var popup = document.getElementById('popup');
document.addEventListener('click', function(e){
  var parent;
  if(!(parent = e.target.closest('#popup'))){
    popup.classList.remove('show');
  }
});
&#13;
/* 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;
    }
&#13;
<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>
&#13;
&#13;
&#13;