我有一个popup div position:fixed
。最初我想显示加载图像,直到内容未加载。
我用过
this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px");
this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
根据屏幕将div居中。它工作正常。
加载内容后出现问题,因为内容的范围从简单文本段落到复杂表单,它们会在右侧和底部方向上展开。由于我的div根据那个小的“加载”图像居中,现在有了新内容,它更倾向于正确和底部。
有什么方法可以在所有4方向扩展div。
答案 0 :(得分:0)
首先,让我们来解决你的div中心问题。对于固定div,请考虑以下事项:
<!DOCTYPE html>
<html>
<head>
<style>
div.center
{
position: relative;
width: 400px;
height: 400px;
margin: 0px auto;
background-color: red;
}
div.fix
{
position: fixed;
width: 100%;
margin: 100px auto;
}
</style>
</head>
<body>
<div class="fix"><div class="center"></div></div>
</body>
</html>
这会使你的div动态居中,无论它的大小如何,或者它如何变化(高度和宽度)。它也非常不精明(我赞成)。尝试将这种类型的CSS应用于您的问题,它应该华丽地调整大小。