如何禁用弹出窗口?

时间:2018-02-21 19:21:17

标签: javascript html css

当我点击窗口上的任何地方时,是否可以关闭弹出窗口。只是纯粹的Javascript。这是JS

   function popItUp() {
       var popup = document.getElementById("myPopup");
       popup.classList.toggle("show");
   }

这是CSS

        .popup .show {
            visibility: visible;
            -webkit-animation: fadeIn 1s;
            animation: fadeIn 1s;
        }


        @-webkit-keyframes fadeIn {
            from {opacity: 0;}
            to {opacity: 1;}
        }

        @keyframes fadeIn {
            from {opacity: 0;}
            to {opacity:1 ;}
        }

这是HTML

       <div class="slidecontainer">
           <div class="popup" onclick="popItUp()">i
               <span class="popuptext" id="myPopup">Some info</span>
           </div>
       </div> 

提前致谢

2 个答案:

答案 0 :(得分:0)

您已经拥有弹出窗口的状态。它是 // try Histogram //using MathNet.Numerics.Statistics; // required header // create random data Random RanGen = new Random(); double[][] d = new double[4][]; for (int i = 0; i < 4; i++) d[i] = new double[100]; for (int i = 0; i < 4; i++) { for (int j = 0; j < 100; j++) d[i][j] = RanGen.NextDouble(); } // create histogram for 3rd array = d[2] ; Histogram h2 = new Histogram(d[2], 10); // write bucket info including max, min and count for (int b = 0; b < 10; b++) Console.WriteLine(h2[b].ToString()); // Write row number and row data for all rows for which d3 value is in bucket with index = 7; for (int j = 0; j < 100; j++) { if (h2.GetBucketIndexOf(d[2][j]) == 7) Console.WriteLine(j + " " + d[0][j].ToString() + " " + d[1][j].ToString() + " " + d[2][j].ToString() + " " + d[3][j].ToString() + " "); } 类。因此,只要.show课程不在show,它就不会显示,对吗?

因此,当您单击窗口上的任何位置时,需要执行的操作是从弹出窗口中删除div类所需的函数。

这是一个带有一些代码的编辑:

show

答案 1 :(得分:0)

您需要检测元素上的click事件。然后阻止它传播到文档。

还可以检测文档任何部分的点击事件,然后切换课程show

click事件从元素传播,然后传播到文档。您需要使用event.stopPropagation()阻止它传播到文档。

&#13;
&#13;
document.onclick = function(){
  document.getElementById('myPopup').classList.remove('show');
};
// When the user clicks on div, open the popup
function myFunction(e) {
    var popup = document.getElementById("myPopup");
    popup.classList.toggle("show");
    e.stopPropagation();
}
&#13;
.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;
}

/* 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 */
/* Toggle this class - hide and show the popup */
.popup .show {
    visibility: visible;
    -webkit-animation: fadeIn 1s;
    animation: fadeIn 1s;
}

/* Add animation (fade in the popup) */
@-webkit-keyframes fadeIn {
    from {opacity: 0;} 
    to {opacity: 1;}
}

@keyframes fadeIn {
    from {opacity: 0;}
    to {opacity:1 ;}
}
&#13;
<body style="text-align:center">

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

</body>
&#13;
&#13;
&#13;