0

I have made a simple slider gallery for my site but have found that when I click next the image updates but it does not centre until I have done a full cycle of the images

how can i get the images to align from the start?

HERE IS THE JS FIDDLE > http://jsfiddle.net/8pScd/4

HTML

<div class="view_gallery">view gallery</div>

<div class="prev control"><<</div>
<div class="next control">>></div>

<div class="gallery">

</div>

<div class="overlay"></div> 

CSS

.overlay{
    display: none;
    position: absolute; top: 0; right: 0; bottom: 0; left: 0;
    width: 100%; height: 100%;
    background: rgba(0,0,0,0.8);
    z-index: 100;
}

.gallery{
    z-index: 200;
    padding: 10px;
    position: absolute;
    left: 50%; 
    background: #fff;
}

.control{
    position: absolute;
    top: 200px;
    z-index: 300;
    color: #fff;
    text-transform: capitalize;
    font-size: 2em;
    cursor: pointer;
}

.prev{left: 0;}
.next{right:0;}

JQUERY

    //images
    var pics = new Array();
    pics[0] = "cars.jpg";
    pics[1] = "cats.png";
    pics[2] = "dogs.png";
    pics[3] = "bus.jpg"

    //total amount of pictures to display
    var pictot = pics.length-1;

    var nxt = $(".next"),
        prv = $(".prev"),
        view = $(".view_gallery"),
        gal = $(".gallery"),
        overlay = $(".overlay"),
        num = 0;

    //view gallery
    view.click(function(){
        overlay.show();
        gal.show();
        // Start gallery off on the first image
        gal.html('<img src="' + pics[0] + '" />');
    });

    nxt.click(function(){
        // If on the last image set value to 0. Else add 1
        if (num == pictot){num = 0;}else{num++;};
        update();
    });

    prv.click(function(){
        // If on first image set value to last image number. Else minus 1
        if (num == 0){num = pictot;}else{num--;}
        update();
    });

    function update () {
        // update image with next/previous
        gal.html('<img src="' + pics[num] + '" />');
        //center image (not working very well)
        var x = gal.width()/2;
        gal.css("marginLeft", -x);
    };

    //hide
    overlay.click(function(){
        gal.hide();
        $(this).hide();
    });
2
  • Maybe you could add text-align:center; to your .gallery style defination.
    – zgood
    Commented Nov 14, 2013 at 14:32
  • unfortunately doesn't work
    – navigator
    Commented Nov 14, 2013 at 14:45

4 Answers 4

1

The problem you have is that the "update" function is called immediately after clicking on prev/next. The image has not yet been loaded, so the code does not actually know the new gal.width yet. That's why it works after a full round: the images are now in the cache, and therefore already available.

The best solution would be to use javascript Image objects to preload the pictures; an easier way but possibly problematic is to use the 'load' event (it may not work well in all browsers).

0

You can align your gallery div with some simple css hack.

1)first define width. (you can define dynamic width with jquery).

2)add position:absolute;

3)add left:0 , right:0;

4)add margin:0 auto;

final code looks like this.

.gallery {
    background: none repeat scroll 0 0 #FFFFFF;
    left: 0;
    margin: 0 auto !important;
    padding: 10px;
    position: absolute;
    right: 0;
    width: 600px;
    z-index: 200;
}
2
  • Thats fine for placing the gallery box in the middle but with a absolute width the images are overflowing the box's i wanted to center align them without changing the image size
    – navigator
    Commented Nov 14, 2013 at 15:14
  • if you want align center an inline element, you must give text-align:center to that's container. in this case , I think .gallery want text-align:center; and img want max-width : 100%;
    – SAFEER N
    Commented Nov 14, 2013 at 15:37
0

your math is wrong, look at this example http://jsfiddle.net/8pScd/6/

i've just need to change your math at

 var x = $('body').width()/2 - gal.width()/2;
 gal.css("margin-left", x + 'px');    

and i removed this line at your css left: 50%;

.gallery{
    z-index: 200;
    padding: 10px;
    position: absolute;
    background: #fff;
}
1
  • my math does exactly the same the css put the div half 50% of the way across the page then i have dynamically taken away half of the div width using margin left
    – navigator
    Commented Nov 14, 2013 at 16:03
0

Knowing that .gallery is 920px wide, set left: 50%; margin-left: -470px. Also remove the line in javascript which updates margin-left of the gallery container - gal.css("marginLeft", -x);

1
  • the gallery isnt a set width, it just has a 10px padding, if the images were all different sizes this would not work
    – navigator
    Commented Nov 14, 2013 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.