【问题标题】:Display Full-Page Overlay on Page Load在页面加载时显示整页叠加
【发布时间】:2020-05-02 15:29:21
【问题描述】:

我想在页面加载时显示整页叠加,并能够通过单击 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>

【问题讨论】:

  • 尝试删除 .overlay css 中的 width: 0 - 我会添加 right: 0 :)
  • 是的,这就像做梦一样。谢谢,@Teknotica
  • 很高兴听到!欢迎你:)

标签: javascript html css


【解决方案1】:
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;
    }

【讨论】:

    【解决方案2】:

    为了使覆盖全屏,根据分辨率,您可以将宽度和高度属性从 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;
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-02
      • 1970-01-01
      相关资源
      最近更新 更多