-2

所以我希望我的 div 背景滚动到窗口的高度,即使你向下滚动。

然而,它似乎只是原始窗口大小的高度。当我向下滚动时,我看到了我身体的背景。

我在这个网站上搜索过类似的问题,我发现的只是使用 height: 100%; 但这不起作用。

这是我的 CSS 代码:

div#graycontainer {
            margin: auto;
            margin-top: 50px;
            width:1000px;
            background-color: #D9D9D9;
            height: 100%;
            /*box-shadow: 10px 10px 7px #888888;*/
            /*border: 3px solid #888889;*/
            border-radius: 1px;
} 

我不相信我的其他代码有任何问题,并且将此站点用作最后的手段。

谢谢你的帮助!

4

1 回答 1

1

使用height: 100%;只会创建具有浏览器高度的图像。您想要做的是不依赖于滚动位置的事情。如果您希望图像在滚动时保持原位,您可以使用position. 第一次使用position: fixed;这会将图像设置在它的位置。之后,您可以使用 z-index 来确保它保持在背景之上。用这个:

#image_at_back {  
  margin: auto;  
  margin-top: 50px;  
  width:1000px;  
  background-color: #D9D9D9;  
  height: 100%;  
  /*box-shadow: 10px 10px 7px #888888;*/  
  /*border: 3px solid #888889;*/  
  border-radius: 1px;  
  position: fixed;  
  z-index: -1; // to make it a background
}

你可以在这里阅读更多。

添加固定位置时,尝试添加一些文本以使滚动条可见。然后,当您滚动它时,图像会停留在其位置。

于 2013-08-17T21:41:41.903 回答