js實現彈框效果

本文實例為大傢分享瞭js實現彈框效果的具體代碼,供大傢參考,具體內容如下

js實現彈窗效果

利用display來控制彈窗的現實和隱藏

<!-- 彈出層 -->
<div id="popLayer"></div>  <!--黑色蒙版 -->
<div id="popBox">
   <div class="close">
      X
   </div>
   <div>
     <!-- 內容 -->
  </div>
</div>

js:

//點擊關閉按鈕
var close = document.querySelector(".close")
close.onclick = function () {
  console.log("點擊")
  var popBox = document.getElementById("popBox");
  var popLayer = document.getElementById("popLayer");
  popBox.style.display = "none";
  popLayer.style.display = "none";
}


//需要顯示時調用
var popLayer = document.getElementById("popLayer");
popBox.style.display = "block";
popLayer.style.display = "block";

CSS:

/* 彈出層 */
#popLayer {
  display: none;
  background-color: #000;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 10;
  opacity: 0.6;
}

/*彈出層*/
#popBox {
  display: none;
  background-color: #FFFFFF;
  z-index: 11;
  width: 220px;
  height: 300px;
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  margin: auto;
}

/*關閉按鈕*/
#popBox .close {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  position: absolute;
  border: 1px solid #fff;
  color: #fff;
  text-align: center;
  line-height: 20px;
  right: 8px;
  top: 8px;
  z-index: 50;
}

#popBox .close a {
  text-decoration: none;
  color: #2D2C3B;
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: