jQuery center image
Posted
by Aaron Moodie
on Stack Overflow
See other posts from Stack Overflow
or by Aaron Moodie
Published on 2010-03-20T08:23:38Z
Indexed on
2010/03/20
8:31 UTC
Read the original article
Hit count: 118
I've got an issue where I was tying to center images using the jQuery Cycle plugin. I found this solution, but it wasn't working on single images (there isn't always > 2 images), so I wrote my own little bit of code, which seems to be working, except it sometimes doesn't subtract the height of the image from the height of the div, and so i end up with a margin of 310px.
var $image_cnt = $("#images > img").size();
if($image_cnt < 2) {
var $single_img = $("#images").children(':first-child');
var h = $single_img.height();
$single_img.css({
marginTop: (620 - h) / 2,
});
$(".next").css("display","none");
$(".prev").css("display","none");
}
I haven't used jQuery much, and just wanted to know if I'd missed something simple, or had written something wrong, which is why the marginTop wasn't playing nice.
© Stack Overflow or respective owner