Skip to main content
updated JS Fiddle
Source Link

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

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

added 54 characters in body
Source Link

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

HTML

HTML

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

HTML

Source Link

how to i get the image to center in my gallery

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?

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();
    });