tags:

views:

40

answers:

2

I have this:

function startLoad(object_id) {
$j('div#'+object_id).addClass('currently-loading');
var con = $j('div#overlay');
var img = $j('div#loadimg');
var tab = $j('table.app');
con.height(tab.height() - 30);
con.width(tab.width());
con.css('display', 'block');

img.css('margin-left', (con.width() / 2 - 36) + 'px');
img.css('margin-top', (con.height() / 2 - 36) + 'px');
//alert( (con.width() / 2 - 36)+" "+(con.height() / 2 - 36))

}

and when i uncomment the alert, it shows the correct dimensions... I have tried left, margin-left, both with and without the px. (some for top)

what am I doing wrong?

everything works, except img is in the top left corner of the div that contains it.

+1  A: 

I've just posted an answer for the same question here.

[See it in action]

HTML

<div id="overlay">
  <img src="http://www.sanbaldo.com/wordpress/wp-content/bigrotation2.gif" 
    id="img-load" />
</div>

CSS

#overlay { 
  display:none; 
  position:absolute; 
  background:#fff; 
}
#img-load { 
  position:absolute; 
}

Javascript

$t = $("#table"); // CHANGE it to the table's id you have

$("#overlay").css({
  opacity : 0.5,
  top     : $t.offset().top,
  width   : $t.outerWidth(),
  height  : $t.outerHeight()
});

$("#img-load").css({
  top  : ($t.height() / 2),
  left : ($t.width() / 2)
});

Then when you're loading things you just say:

$("#overlay").fadeIn();

And when you finished

$("#overlay").fadeOut();
galambalazs
so, i changed my img modifications to this $t = $j('div#overlay'); img.css({ top : ($t.width() / 2 - 36), left : ($t.height() / 2 - 36) });i really like the syntax, but my image is still in the top left =\
DerNalia
see the **whole** example, and change the code accordingly.
galambalazs
as it turns out, I was using the wrong id name. lol.anyway... now i just need to fix my math, so its centered. alert(con.width()+" "+con.height()+"--"+(con.width() / 2 - 36)+" "+(con.height() / 2 - 36))for whatever reason, dividing by two doesn't mean half?
DerNalia
your math looks fine. Make sure to use `position:absolute` for both `#overlay` and `#loadimg`.
galambalazs
also use `tab.outerWidth()` and height because it includes padding, while `width()` doesn't. But as i said, **see my answer** and change accordingly...
galambalazs
A: 

for using left and top you need to position the image absolutely.

position:absolute;

also the container element should have position:relative; set on it

use this and try top, left instead of margin-top and margin-left in your code;

naiquevin
No, he could use `position: relative` too.
Brock Adams
yes position :relative on the child would also be fine.
naiquevin