我有这个:
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))
当我取消注释警报时,它会显示正确的尺寸... 我试过左边,左边距,有和没有px。 (一些用于顶部)
我做错了什么?
一切正常,除了 img 位于包含它的 div 的左上角。
最佳答案
我刚刚发布了同一个问题的答案 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)
});
然后当您加载内容时,您只需说:
$("#overlay").fadeIn();
当你完成的时候
$("#overlay").fadeOut();
关于jQuery:我如何定位图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3215283/