I'm attempting to create jQuery Cycle slideshow where images of different sizes are vertically and horizontally centered, and also re-size to whatever the window size is. Some of it is straight css but I need to dynamically adjust 'top', 'left', 'margin-top', and 'margin-left' on each image before fading in the first image of the slideshow.
Thus I believe need to create a callback where the dimensions and margins are set before the images appear. Can't figure out the best way of doing this.
CSS is simple. Essentially:
#gallery img {
max-height: 100%;
position:absolute;
display:none;
}
And a rough cut of the .js would be:
$(window).bind("load resize", function() {
$('#gallery img').each(function(i) {
var ih = $(this).height();
var iw = $(this).width();
var fh = Math.ceil(ih / 2);
var fw = Math.ceil(iw / 2);
$(this).css({
'top' : '50%',
'left' : '50%',
'margin-top' : '-' + fh + 'px',
'margin-left' : '-' + fw + 'px'
});
},function() {
$('#gallery img:first').fadeIn(3000, function() {
$('#gallery').cycle();
});
However I'm not seeing the expected result. The fadeIn of the first img triggers before the calculations and positioning occurs.
Thinking that 'display:none' might confuse things (maybe nothing is calculated until it's 'display:block', I've tried using:
#gallery img:first-child {
display:block;
opacity:0;
}
..And then animating to full opacity instead of the fadeIn--but the result is the same.
What am I doing wrong??