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
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.
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.
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With