如何在CSS中用页面的完整高度填充div? (页面高度超过100%)ajax加载gif背景
好吧,有几个类似的问题,但不完全是我想要的。
我在页面上有几个ajax请求,我想在屏幕中央显示图像,并且一切正常。
为了让它看起来更突出,我想将该图像放置在具有半透明背景的 div 上,这样对于最终用户来说更明显。现在是棘手的部分。
我用 css 制作了这样的 div:
.divLoadingBackground
{
filter: Alpha(Opacity=40); -moz-opacity:0.4; opacity: 0.4;
width: 100%;
height: 100%;
background-color: #333;
position: fixed;
top: 0px;
left: 0px;
}
这填充了页面,或者,我应该说,这填充了视口。如果我向下滚动页面,页面又恢复正常。我希望这个 div 跨越页面的整个长度,无论页面有多长。
这是我为了快速演示而制作的问题的示例模型:
如您所见,我举了以下示例对于样机来说也是如此;)图像 1 显示它出现时没问题。图 2 显示它随着页面滚动而上升。 我是 ac# 开发人员,CSS 对我来说就像古代拉丁语一样陌生。
如何让这个divLoadingBackground div填满页面的整个长度?
非常感谢您的帮助。 如果您需要任何其他信息,请评论!
ok there are several similar questions but not quite anything that I want.
I have few ajax requests on page and I want to show the image in the center of the screen, and its all working OK.
Just to make it look more prominent, I wanted to place that image on a div with translucent background, so its more obvious for the end users. Now comes the tricky part.
I made the div with css like this:
.divLoadingBackground
{
filter: Alpha(Opacity=40); -moz-opacity:0.4; opacity: 0.4;
width: 100%;
height: 100%;
background-color: #333;
position: fixed;
top: 0px;
left: 0px;
}
This fills the page up alright, or, I should say, this fills the viewport. If I scroll the page down, the page is again normal. I want this div to span the ENTIRE LENGTH of the page, no matter how long the page is.
Here is an example mockup of the problem I made to quickly demonstrate:
As you can see, I took the example of SO for the mockup ;) image 1 shows that its okay when it appears. image 2 shows that it goes up with the page on scroll.
I'm a c# developer and css is as alien to me as ancient latin.
How to make this divLoadingBackground div to fill out the entire length of the page?
Many thanks for any help.
If you need any additional info, please comment!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我在你的 css 中没有看到的一件事是 z-index。已修复,虽然修复了此问题,但有时,根据其他 div 的定位方式,您的 divLoadingBackground div 可能最终出现在其中一个 div 中。
尝试添加
或类似的东西,看看它是否有效。
One thing I dont see in your css is z-index. Fixed, although, fixes this problem, sometimes, based on how other divs are positioned, your divLoadingBackground div could end up in one of the divs.
try adding
or something similar and see if it works.
本来可以将此放在评论中,但似乎我的代表太低,无法发表评论。
.divLoadingBackground div 位于 DOM 树中的什么位置?由于它具有固定位置,因此它不应随页面滚动。这让我相信该元素嵌套得太深。尝试将其放在页面的正文级别,看看是否有帮助。
另外,您确定其他一些 css 指令不会将位置属性更改为绝对或其他属性吗?
另外,请确保使用正确的 DOCTYPE。这对固定位置元素有一些影响。
哦,当然,IE6 及以下版本不支持固定位置。
Would have put this in a comment, but it seems I have too low rep to comment.
Where is the .divLoadingBackground div located in the DOM tree? Since it has fixed position, it shouldn't scroll with the page. This makes me belive that the element is too deeply nested. Try putting it right in the body level of the page and see if that helps.
Also, are you sure that some other css directive isn't changing the position attribute to absolute or something?
Also, make sure to use the right DOCTYPE. That has some impact on fixed position elements.
Oh, and ofcourse, fixed position isn't supported in IE6 and below.
我相信您将需要 JavaScript/jQuery 来动态地将相关 div 的高度设置为渲染后页面的高度。
如果您正在进入网络世界,那么是时候学习新语言“CSS”以及 Perpahs(不那么令人畏惧的 JavaScript)了。
I believe you will need JavaScript/jQuery to dynamically set the height of the div in question to the height of the page once rendered.
And if you're entering the world of web, it's time to learn that new language "CSS" as well as perpahs-not-quite-as-daunting JavaScript.
几年前,当我需要这样的功能时,我研究了 Google 日历 是如何实现的。
基本上,它们使用计时器驱动的 JavaScript 文件来检查窗口的高度并相应地调整所包含的 DIV 标记(或 IFRAME 标记,您喜欢的任何容器标记)的高度。
以下是我所处理的页面中的代码片段:
基本上,脚本通过 GetClientHeight() 函数定期检查窗口的高度,并相应地调整相关元素(“iframecontent”)。
我减去固定高度页眉和页脚的一些偏移量。
When I needed such a functionality some years ago, I examined how Google Calendar did it.
Basically, they use a timer-driven JavaScript file that checks for the height of the window and adjust the height of a contained DIV tag accordingly (or of an IFRAME tag, just any container tag that you like).
Here is a code snippet from a page I worked on:
Basically, the script regularly checks for the height of the window via the
GetClientHeight()
function and adjusts the element in concern ("iframecontent") accordingly.I subtract some offsets of fixed-height headers and footers.
据我所知,您需要通过 javascript 设置此
div
的大小。我建议使用 jQuery,这样:AFAIK you would need to set the size of this
div
through javascript. I would recommend using jQuery, in this way :