【问题标题】:Close this popup by clicking away from it通过单击远离它来关闭此弹出窗口
【发布时间】:2017-11-23 03:12:44
【问题描述】:

我一直在尝试通过单击远离它来关闭此弹出窗口,但没有成功。目前只有直接点击才会关闭。

提前感谢您的帮助。

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

提前感谢您的帮助。

【问题讨论】:

  • 哪个是您的弹出窗口。没有意义!

标签: javascript css popup


【解决方案1】:

最简单的方法是将侦听器添加到文档中,然后如果单击不是来自任何弹出子项,则切换(或做任何您需要的事情)

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>

【讨论】:

  • 感谢 Gacci 的意见。当单击页面的任何部分时,您的解决方案似乎会导致弹出窗口出现?解决了一个问题,但我认为我们创建了另一个问题?
  • 这只是为您指明方向的示例。你的例子缺乏目标。你想要达到的目标没有明确的方向!从那里很容易做任何你想做的事!你想要的行为是什么
  • 将其更改为在加载时显示,然后在单击不是弹出窗口本身的任何位置时消失!
【解决方案2】:

我假设您的弹出窗口有一个带有#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>

【讨论】:

  • 我是个新手,您能否在 sn-p 中向我展示您的解决方案,以便我完全按照您的建议进行操作?谢谢金吉。 R
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-27
  • 2020-06-29
相关资源
最近更新 更多