简体   繁体   English

如何在背景停留时使div内容滚动

[英]how do I make the div content to scroll while the background stays

Ok so what I mean is, I want my background image to stay and the content in the div to scroll as more content inside is added. 好的,所以我的意思是,我希望保留背景图像,并随着添加更多内容而使div中的内容滚动。

see I don't want this to scroll http://codepen.io/anon/pen/gLCns/ 看到我不想让它滚动http://codepen.io/anon/pen/gLCns/

see kind of like the content on the codepen where you scroll in each window but it doesn't flow all over just in that window 在每个窗口中滚动时都看到类似代码笔上的内容,但是它并不仅在那个窗口中流过

you can use background-attachment: fixed; 您可以使用background-attachment: fixed; property to fix the background image. 固定背景图片的属性。

html { 
width: 100%;
height: 100%;
background: url(http://lorempixel.com/400/400) no-repeat center center fixed; 
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}

.content{
position: absolute;
background-color: rgba(255,255,255,0.7);
width:50%; 
height:1020px;
left:20px;
top:20px;
}

Here is a Demo. 这是一个演示。

The background-attachment property is what controls if the background image scrolls or stays. background-attachment属性是控制背景图像滚动或停留的属性。

http://www.w3schools.com/cssref/pr_background-attachment.asp http://www.w3schools.com/cssref/pr_background-attachment.asp

So in the CodePen it has background-attachment:fixed; 因此,在CodePen中,它具有background-attachment:fixed; and the image stays put while the content above it scrolls. 并且当其上方的内容滚动时,图像保持原状。

Then you simply center the content container on the page, leaving off overflows, and as the content grows the page will scroll but the background is fixed. 然后,您只需将内容容器放在页面上居中,避免溢出,并且随着内容的增长,页面将滚动,但是背景是固定的。

OK, first your code is a mess. 好的,首先您的代码是一团糟。 I recommend running your code through the w3 validator first. 我建议您首先通过w3验证器运行代码。

You have two options to do what you want, either using the background fixed & cover that you already have answers for: 您有两种选择来做您想做的事情,一种是使用背景固定的 封面 ,而背景已经覆盖了您的答案:

html {
width: 100%;
height: 100%;
background: url(image_URL) no-repeat center center fixed; 
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}

or using overflow on the div with the content. 或在内容上使用div上的溢出

#content {
width: 600px;
height: 500px;
overflow-y: scroll;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当背景在html / css中保持固定时,如何滚动表格? - How do I get my table to scroll, while the background stays fixed in html/css? 如何使用HTML的滚动内容制作响应式div? - How do I make a responsive div with scroll content in HTML? 我希望固定DIV中的背景,并且仅内容随滚动移动,该怎么办? - I want a background inside DIV to be fixed, and only the content to move with scroll, how can I do it? 如何使div具有背景图片且没有内容显示并从页面的顶部延伸到底部? - How do I make my div with a background image and no content show and extend from the top to bottom of the page? 如何制作一个 div static 和第二个 div 滚动? - How do I make one div static and the second div scroll? 如何使div在不使用jquery滚动时保持原状? - how to make a div stays in place while scrolled without jquery? 无论您在页面上的什么位置,如何使div保持固定位置? - How do I make a div that stays in a fixed position regardless of where you are on the page? 如何获得div背景图片滚动? - How do I get a div background image to scroll? 如何在旋转图像 CSS 或 Javascript 时使背景图像填充空白区域(在 div 上)? - How do I make the background image to fill the white spaces (on div) while rotating the image CSS or Javascript? 如何滚动背景图像而不在页面上平铺? - How do I make a background image scroll and not be tiled on the page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM