使用javascript激活css弹出窗口

时间:2017-06-14 22:45:46

标签: javascript css

您好我有这个CSS弹出窗口,当前设置了一个按钮。我也有这个JavaScript"鼠标点击计数器"。我想要发生的是当计数器变为零时弹出窗口显示出来。我觉得很简单,我只是没有足够的JavaScript经验。感谢

CSS Popup

.overlay:target {
  visibility: visible;
  opacity: 1;
}

.popup {
  margin: 70px auto;
  padding: 20px;
  background: #fff;
  border-radius: 5px;
  width: 30%;
  position: relative;
  transition: all 5s ease-in-out;
}

.popup h2 {
  margin-top: 0;
  color: #333;
  font-family: Tahoma, Arial, sans-serif;
}
.popup .close {
  position: absolute;
  top: 20px;
  right: 30px;
  transition: all 200ms;
  font-size: 30px;
  font-weight: bold;
  text-decoration: none;
  color: #333;
}
.popup .close:hover {
  color: #06D85F;
}
.popup .content {
  max-height: 30%;
  overflow: auto;
}

@media screen and (max-width: 700px){
  .box{
    width: 70%;
  }
  .popup{
    width: 70%;
  }
}

JavaScript鼠标单击计数器(从10开始,然后从那里开始)

var number = 10;

            document.onclick = function(){
                    number --;
                    document.getElementById("clicks").innerHTML = number;
            }

我倾向于不澄清事情,只是为了确定。一旦计数器降到零,我想弹出以激活所有功能。包括屏幕不透明度。谢谢!

jsfiddle - 鼠标点击计数器 here

jsfiddle - 弹出 here

1 个答案:

答案 0 :(得分:1)

以下是检查number的值的方法,如果#clicks增加number > -1,如果number == 0,则更新哈希,以便显示您的弹出窗口。< / p>

var number = 10;

document.onclick = function() {
  number--;
  if (number > -1) {
    document.getElementById("clicks").innerHTML = number;
    (number == 0) && (location.hash = '#popup1')
  }
};
.overlay {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.7);
  transition: opacity 500ms;
  visibility: hidden;
  opacity: 0;
}
.overlay:target {
  visibility: visible;
  opacity: 1;
}

.popup {
  margin: 70px auto;
  padding: 20px;
  background: #fff;
  border-radius: 5px;
  width: 30%;
  position: relative;
  transition: all 5s ease-in-out;
}


.popup h2 {
  margin-top: 0;
  color: #333;
  font-family: Tahoma, Arial, sans-serif;
}
.popup .close {
  position: absolute;
  top: 20px;
  right: 30px;
  transition: all 200ms;
  font-size: 30px;
  font-weight: bold;
  text-decoration: none;
  color: #333;
}
.popup .close:hover {
  color: #06D85F;
}
.popup .content {
  max-height: 30%;
  overflow: auto;
}

@media screen and (max-width: 700px){
  .box{
    width: 70%;
  }
  .popup{
    width: 70%;
  }
}
<div id="clicks"></div>
<div id="popup1" class="overlay">
	<div class="popup">
		<h2>WOW!</h2>
		<a class="close" href="#">&times;</a>
		<div class="content">
      <center>Lets see how you did!!</center>
		</div>
	</div>
</div>