Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an overlay DIV to cover an existing DIV (using JQuery)?

Tags:

I have a 'container' DIV which may have all possible CSS styles like: margin, padding, border and different position (fixed, relative, absolute).

I want to show a loading icon above the 'container' DIV and forbid the user to operate any control in the 'container' DIV.

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

How can I add an overlay DIV (using JQuery) which covers the entire 'container' DIV visible area (margin area should not be covered)?

Best regards, Zach

like image 660
Zach Avatar asked Dec 07 '12 01:12

Zach


People also ask

How can show overlay div in jQuery?

An overlay is, simply put, a div that stays fixed on the screen (no matter if you scroll) and has some sort of opacity. This will be your jQuery code (no UI needed). You're just going to create a new element with the ID #overlay. Creating and destroying the DIV should be all you need.

How do I overlay one div over another div?

By using a div with style z-index:1; and position: absolute; you can overlay your div on any other div . z-index determines the order in which divs 'stack'. A div with a higher z-index will appear in front of a div with a lower z-index . Note that this property only works with positioned elements.

How do I overlay a div over an image?

Use z-index and top . This will layer the div on bottom, the image and then the span (overlay) on top. To set the positioning from the top edge, use top , which can be used with negative numbers if you need it to be higher on the Y axis than it's parent.


1 Answers

If nothing to change in CSS, then:

$("<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/

like image 97
VisioN Avatar answered Sep 18 '22 15:09

VisioN