在页面加载时显示全页面覆盖

时间:2019-08-24 09:33:27

标签: javascript html css

我想在页面加载时显示整页的覆盖图,并具有通过单击X来关闭覆盖图的功能。因此,默认情况下,叠加层会在页面加载时显示,然后被关闭,然后显示网站的其余部分(此处未包含代码)。

我已经尝试了以下方法,但不确定为什么它不起作用:

function openNav() { 
  document.getElementById("myNav").style.width = "100%";
}
.overlay {
  height: 100%;
  width: 0;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  background-color: rgb(0,0,0);
  background-color: rgba(0,0,0, 0.9);
  overflow-x: hidden;
  transition: 0.5s;
}
.overlay-content {
  position: relative;
  top: 25%;
  width: 100%;
  text-align: center;
  margin-top: 30px;
}
.overlay a {
  padding: 8px;
	text-decoration: none;
	font-size: 36px;
	color: #818181;
	display: block;
	transition: 0.3s;
}
.overlay a:hover, .overlay a:focus { color: #f1f1f1; }
@media screen and (max-height: 450px) {
  .overlay a {font-size: 20px}
  .overlay .closebtn {
    font-size: 40px;
		top: 15px;
		right: 35px;
  }
}
<div id="myNav" class="overlay">
  <div class="overlay-content">
    <a href="#">Something</a>
  </div>
</div>

2 个答案:

答案 0 :(得分:0)

Kindly Replace ".overlay" css with this:

.overlay {
      height: 100%;
      width: 100%;
      position: fixed;
      z-index: 1;
      top: 0;
      left: 0;
      background-color: rgb(0, 0, 0);
      background-color: rgba(0, 0, 0, 0.9);
      overflow-x: hidden;
      transition: 0.5s;
    }

答案 1 :(得分:0)

要使覆盖图全屏显示,请根据分辨率,将width和height属性从px更改为vh和vw。 例如:

.overlay {
  height: 100vh;
  width: 100vw;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  background-color: rgb(0,0,0);
  background-color: rgba(0,0,0, 0.9);
  overflow-x: hidden;
  transition: 0.5s;
}

这样,您不必担心屏幕分辨率。完美适合任何类型的屏幕。

相关问题