Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery animate to next image in div

I've got some images within a div which i'm trying to when I click the next button animate to the next one in the div

In this div I also have my previous and next button which I obviously don't want to animate to.

<div class="work">
<a href="#" class="rightButton"><img src="http://www.jamietaylor-webdesign.co.uk/images/home/right.png" /></a>
<a href="#" class="leftButton"><img src="http://www.jamietaylor-webdesign.co.uk/images/home/left.png" /></a>
<a href="#" class="1"><img class="topLeft" src="http://i.stack.imgur.com/JQFbb.jpg" /></a>
<a href="#" class="2"><img class="topRight" src="http://i.stack.imgur.com/l5OPs.jpg" /></a>
<a href="#" class="3"><img class="bottomLeft" src="http://i.stack.imgur.com/okxQz.jpg" /></a>
<a href="#" class="4"><img class="bottomRight" src="http://i.stack.imgur.com/4uPHw.jpg" /></a>
</div>

My jQuery which doesn't actually work

    // prevent click jump
$('a').click(function() {
    return false;
});

// do work
$('.work > .leftButton').click(function() {

    //animate to previous image in the list
    $(this).siblings().prev().show();

});

$('.work > .rightButton').click(function() {

    //animate to next image in the list
    $(this).siblings().next().show();

});

Is there a way to animate to the next image that doesn't have the class of leftButton or rightButton.

Also is there a way so that if i'm on the first image it will go to the last image in the div?

Here's a link to the jsFiddle i've got

Any ideas?

like image 371
Jamie Taylor Avatar asked Jan 20 '23 10:01

Jamie Taylor


1 Answers

Edited to scroll images into view:

This requires some change to your markup, but will allow images to scroll left/right:

HTML:

<div class="work">
    <div class="nav">
        <a href="#" class="rightButton"><img src="http://www.jamietaylor-webdesign.co.uk/images/home/right.png" /></a>
        <a href="#" class="leftButton"><img src="http://www.jamietaylor-webdesign.co.uk/images/home/left.png" /></a>       
    </div>
    <div class="scroller">
        <div class="items">
            <a href="#" class="1"><img class="topLeft" src="http://i.stack.imgur.com/JQFbb.jpg" /></a>
            <a href="#" class="2"><img class="topRight" src="http://i.stack.imgur.com/l5OPs.jpg" /></a>
            <a href="#" class="3"><img class="bottomLeft" src="http://i.stack.imgur.com/okxQz.jpg" /></a>
            <a href="#" class="4"><img class="bottomRight" src="http://i.stack.imgur.com/4uPHw.jpg" /></a>
        </div>
    </div>
</div>

Relevant CSS:

div.work { background-color: #ddd; height:240px; position: relative; width:300px;  margin-left:10px}
div.items { position:relative; width: 1000em; height: 240px; }
div.scroller { overflow: hidden; width: 300px; height: 100%; }

JavaScript:

$('.work .leftButton').click(function() {
    var $items = $(".items");
    if ($items.position().left === 0) {
        $items.css("left", "-300px");
        $items.children("a:last").prependTo($items);
    }
    $(".items").animate({
        left: "+=300px"
    });
});

$('.work .rightButton').click(function() {
    var $items = $(".items");
    if ($items.position().left === -900) {
        $items.css("left", "-600px");
        $items.children("a:first").appendTo($items);
    }

    $(".items").animate({
        left: "-=300px"
    });

});

Basically what's going on is that there's a fixed viewport (scroller) with hidden overflow that contains a really long div (items) containing all the images. The items div is just animated on every click of the next/previous buttons.

The tricky part is moving the last element to the front and vice/versa when the end of the line of images is reached. This is determined by hardcoded values, but could probably be improved using an exact calculation.

Working example: http://jsfiddle.net/andrewwhitaker/6TLK2/3/

like image 121
Andrew Whitaker Avatar answered Jan 31 '23 05:01

Andrew Whitaker