如何添加覆盖DIV以覆盖现有的DIV(使用JQuery)?

时间:2012-12-07 01:28:08

标签: jquery overlay

我有一个'容器'DIV,它可能包含所有可能的CSS样式:边距,填充,边框和不同的位置(固定,相对,绝对)。

我想在'容器'DIV上方显示一个加载图标,并禁止用户操作'容器'DIV中的任何控件。

<div class="container">
 A lot of content here ...
</div>

如何添加覆盖整个“容器”DIV可见区域的覆盖DIV(使用JQuery)(不应覆盖边缘区域)?

祝你好运, 扎克

1 个答案:

答案 0 :(得分:54)

如果CSS中没有任何改变,那么:

$("<div />").css({
    position: "absolute",
    width: "100%",
    height: "100%",
    left: 0,
    top: 0,
    zIndex: 1000000,  // to be on the safe side
    background: "url(/img/loading.gif) no-repeat 50% 50%"
}).appendTo($(".container").css("position", "relative"));

$("<div>Loading...</div>").css({
  position: "absolute",
  width: "100%",
  height: "100%",
  top: 0,
  left: 0,
  background: "#ccc"
}).appendTo($(".container").css("position", "relative"));
.container {
  border: 1px solid;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<div class="container">
  A lot of content here ...
</div>

DEMO: http://jsfiddle.net/jKfTC/