views:

29

answers:

3

I'm building a simple image gallery with jquery but running into an annoying problem. Since the images are of various sizes I have centered them all to make it look even. However the built in fadeIn/fadeOut methods throw this centering awry and I'm not completely sure what is going on. I tried manually adding the centering class again then manually adding css but cannot get the image to center once it has been turned visible. Thoughts?

css -

.center { margin: 0 auto; }
img.invisible { display: none; }
img.visible { margin: 0 auto; display: block;}

markup -

<div id="content" class="center gallery">

    <img src="images/event/event_1.jpg" alt="alt-text" class="visible" />
    <img src="images/event/event_2.jpg" alt="alt-text" class="invisible" />
    <img src="images/event/event_3.jpg" alt="alt-text" class="invisible" />

    <div id="selection" class="overlay">
        <div class="select first">
            <a href="#" rel="1"><img src="images/event/event_1_small.jpg" /></a>
        </div>
        <div class="select">
            <a href="#" rel="2"><img src="images/event/event_2_small.jpg" /></a>
        </div>
        <div class="select">
            <a href="#" rel="3"><img src="images/event/event_3_small.jpg" /></a>
        </div>
    </div> 

jQuery -

function updateImage(img_num) {
    var cur_img = $("#content img.visible");
    var next_img = $('#content img[src^="' + img_path + img_num + '.jpg"]');

    cur_img.fadeOut('600',function() {
        next_img.fadeIn('600');
        next_img.removeClass("invisible").addClass("visible");
        cur_img.removeClass("visible").addClass("invisible");
    });
}
A: 

You are adding margin: 0 auto; only to the .visible class, you need to apply that to all of your images:

.gallery img{margin:0 auto;display:none}
.gallery img.visible{display:block}
David
Well the .visible class is the only one that needs to be centered and is visible so the distinction shouldn't make a difference.I tried this method though and there was no change. The jQuery fadeIn method is changing something else.
ChrisOPeterson
A: 

Okay well that was surprising. To fix this problem I tried using fadeTo which revealed that the problem was the images once being made visible were given display: inline; so all it took to fix this problem was.

.gallery { text-align: center; }

I thought jQuery was just changing the opacity but it appears to also change the display. Tricky.

ChrisOPeterson
A: 

mmm i wonder why u aren't just using the lightbox plugin?

http://leandrovieira.com/projects/jquery/lightbox/

elitepraetorian
Because my needs are different and I wanted to build my own because i'm trying to learn javascript/jQuery.
ChrisOPeterson