html - 让页面背景随用户滚动

标签 html css background

我无法让背景随用户滚动。我希望顶部的 12% 为白色,底部的 6% 为白色,屏幕的其余部分为蓝色。我希望顶部的白色仅在用户位于页面顶部时显示,而底部的白色仅在用户位于页面底部时显示。

Here's an image of the page, zoom in to fit your screen then scroll so you get the idea of what I'm going for.

如有任何帮助,我们将不胜感激。

    <body>
<div id="bg-top"></div>
</body>
<style>

body {
  background-color: #ffffff;
  position: scrolling;
}
#bg-top {
  background-color: #00b9ff;
  position: scrolling;
  top: 12%;
  position: fixed;
  bottom: 6%;
  left: 0%;
  right: 0%;
  z-index: -1;
}


</style>

最佳答案

您需要使用位置 absolute 而不是像这样的 fixed :

为了更好地查看结果,我在 body 的高度上设置了一个很大的值

body {
  margin:0;
  padding:0;
  background-color: #ffffff;
  position: relative;
  min-height:1000px;
}

#bg-top {
  background-color: #00b9ff;
  top: 12%;
  position: absolute;
  bottom: 6%;
  left: 0%;
  right: 0%;
  z-index: -1;
}
<div id="bg-top"></div>

关于html - 让页面背景随用户滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47376910/

相关文章:

java - 如何使用JAVA Apache POI为powerpoint的每张幻灯片的背景设置不同的图像?

html - CSS 边框顶部不起作用

html - 如何将元素与垂直线对齐

javascript - 如果在元素外部单击,则删除类

css - html5 教程 >30 设置默认和进度条的样式很奇怪?

html - 我们如何使用线性渐变创 build 计背景?

php - 如何从 Windows 中的 PHP 函数 exec() 获取 PID?

html - 文本没有正确排列?

html - 如何将位于另一个 div 中的 div 居中?

html - 如何使用 CSS 将自定义字体插入到文本中?