tags:

views:

1254

answers:

7

how can i create a really basic overlay in jquery without UI ?

which is a lightweight plugin ?

+1  A: 

If you're already using jquery, I don't see why you wouldn't also be able to use a lightweight overlay plugin. Other people have already written some nice ones in jquery, so why re-invent the wheel?

Dan Breen
which lightweight overlay plugin is there ?
aoghq
Why borrow a wheel with bells and whistles when you can create a perfectly acceptable wheel in 3 lines of code? Plugins aren't always the best solution.
Joel Potter
3 lines of code that might work nicely in FF, but then there might be quirks in some version of IE. At least with a known tool, most of the kinks have already been worked out.
Dan Breen
If you suggest using a plugin you should suggest one or more that you find suitable. Otherwise the answer is not really helpful ...
Hinek
@Hinek - he rephrased the question after I answered. He originally asked for an overlay without using a library and I was suggesting it wasn't much more overhead to use one rather than get frustrated implementing one from scratch.
Dan Breen
A: 

By overlay do you mean content that overlaps/covers the rest of the page? In HTML, you could do this by using a div that uses absolute or fixed positioning. If it needed to be generated dynamically, jQuery could simply generate a div with the position style set appropriately.

Jacob
A: 

Is this what you are looking for? I also wanted a simple image overlaying with jquery and came up with that. You might not need any plugin to do simple overlaying. HTH.

jpartogi
+2  A: 

try facebox or fancybox

gpilotino
A: 

What do you intend to do with the overlay? If it's static, say, a simple box overlapping some content, just use absolute positioning with CSS. If it's dynamic (I believe this is called a lightbox), you can write some CSS-modifying jQuery code to show/hide the overlay on-demand.

Illianthe
+1  A: 

jqModal has always been my favorite light-weight overlay plugin.

Corey Hart
+8  A: 

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 you CSS. Say the overlay has .5 opacity (cross browser).

#overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: #000;
    filter:alpha(opacity=50);
    -moz-opacity:0.5;
    -khtml-opacity: 0.5;
    opacity: 0.5;
    z-index: 10000;
}

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.

var overlay = jQuery('<div id="overlay"> </div>');
overlay.appendTo(document.body)

For performance reasons you might wanna have the DIV hidden and setting the display to block and none as you need it or not.

Hope it helps!

Edit: As @Vitaly so well put it, be sure to check your DocType. Read more on the comments on his findings..

Frankie
This doesn't work in IE8. The "overlay" is shown below content. Any ideas how to fix this?
Vitaly
@Vitaly are you putting the overlay DIV on the top of the page? with `position:fixed; top:0; left:0` if so let me know and I'll give it a look when I get to the office.
Frankie
Yes, just like you that. Setting position to "absolute" makes it work, but it doesn't cover the right edge of the document and scrolls with the rest of the content.
Vitaly
Got it. Had to change doctype from <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.1 Transitional//EN"> to<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"> to make IE understand "position: fixed".
Vitaly
Nice! Thanks! I'm gonna share your discovery on the question to make it easier on Googler's passing by! ;)
Frankie
No problem. Thank YOU.
Vitaly
what if you cant change the doctype because its a remote page - any ideas how to fix it?
Tobias
@Tobias this is the easy, fast, css solution. I guess you could probably try something javascript based.
Frankie