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

问题描述 投票:18回答:1

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

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

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

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

最好的问候,扎克

jquery overlay
1个回答
57
投票

如果没有什么可以改变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>

但是:Kua zxsw指出

© www.soinside.com 2019 - 2024. All rights reserved.