I have a small problem that, in my opinion, can be solved by rethinking my CSS.
On part of the page I have a series of images. Initially, each image is hidden (see below). I am fine. When the page loads, the first image disappears through jquery. A timer is set there, where after X seconds the current image disappears, and the next disappears.
Each image is contained in a larger div. It is assumed that the image is exactly in the middle of the div (vertically and horizontally). Since each image can have a different size, I had to write some logic to check the height of the image and adjust its fields on the fly (Again, see the code below). And it works great, except for the very first image. The calculated height of the first image is always 0 ..., which makes it lower than the middle of the div.
Is it possible to achieve the same effect using vertical alignment? So far, I have not been successful.
Any help is greatly appreciated.
thanks
THE CODE
Markup
<div id="logowrapper"> <div class="logo"> <img src="" /> </div> <div class="logo"> <img src="" /> </div> <div class="logo"> <img src="" /> </div> </div>
CSS
#logowrapper { border-right: 2px solid #DDD; display: block; position: absolute; top: 12px; left: 10px; text-align: center; width: 370px; height: 208px; background-color: #FFF; } .logo { display: none; }
Js
$(document).ready(function() { var currentLogo = 0; var numLogos = $(".logo").size(); var interval = 3000; var boxHeight = $("#logowrapper").height(); function updateLogo() { if (currentLogo >= numLogos) { currentLogo = 0; } $(".logo").hide(); var thisLogo = $(".logo:eq(" + currentLogo + ")"); thisLogo.fadeIn(); var logoHeight = thisLogo.height(); var newMargin = parseInt((boxHeight / 2), 10) - parseInt((logoHeight / 2), 10); thisLogo.css({ marginTop: newMargin }); currentLogo++; } setInterval(updateLogo, interval); updateLogo(); });
source share