html - 滚动时的视差效果问题

标签 html css parallax

我想要一个适合容器元素的图像,但我的代码只显示图像的一半,没有所需的视差效果。谁能解释为什么它不显示完整图像?

* {
  margin: 0;
  padding: 0;
}
.top_nav {
  height: 80px;
  width: 100%;
  background: rgba(0, 0, 0, .5);
  position: absolute;
}
.container {
  height: 400px;
  width: 100%;
  max-width: 100%;
  overflow: hidden;
  background-image: url("http://cdn.pcwallart.com/images/beautiful-nature-animals-wallpaper-3.jpg");
  background-attachment: fixed;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  z-index: -1;
}
.details {
  height: 638px;
  width: 100%;
  background-color: #00e5de;
}
<header>
  <div class="top_nav">

  </div>
</header>
<div class="container">
  <div id="short-des">

  </div>
</div>
<div class=details>
</div>

enter image description here

最佳答案

使用 100vh 作为您的 .container 高度

* {
  margin: 0;
  padding: 0;
}
.top_nav {
  height: 80px;
  width: 100%;
  background: rgba(0, 0, 0, .5);
  position: absolute;
}
.container {
  height: 100vh;
  width: 100%;
  max-width: 100%;
  overflow: hidden;
  background-image: url("http://cdn.pcwallart.com/images/beautiful-nature-animals-wallpaper-3.jpg");
  background-attachment: fixed;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  z-index: -1;
}
.details {
  height: 638px;
  width: 100%;
  background-color: #00e5de;
}
<header>
  <div class="top_nav">

  </div>
</header>
<div class="container">
  <div id="short-des">

  </div>
</div>
<div class=details>
</div>

vh:视口(viewport)高度的百分之一。

关于html - 滚动时的视差效果问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40035434/

相关文章:

jquery - CSS 视差背景

html - 使用 JSON 传递 HTML

css - 有什么方法可以使用纯 CSS 来制作按比例的边距吗?

html - 垂直对齐中间表格单元格响应文本

css - 使用带视差滚动的 anchor

jquery - iOS touchmove 事件 - 改变 backgroundPosition 不流畅

javascript - PHP 和表单处理事件

javascript - 如何更改脚本的字体大小?

php - 无法从数据库中提取并在 PHP 中显示

css - sass中的cos函数返回未定义的操作错误