开发者

jQuery: how do I position an image?

开发者 https://www.devze.com 2023-01-06 10:59 出处:网络
I have this: function startLoad(object_id) { $j(\'div#\'+object_id).addClass(\'currently-loading\'); var con = $j(\'div#overlay\');

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;

0

精彩评论

暂无评论...
验证码 换一张
取 消