jQuery: how do I position an image?
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 t开发者_如何学Che div that contains it.
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();
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;
精彩评论