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?
Edited to scroll images into view:
This requires some change to your markup, but will allow images to scroll left/right:
HTML:
Relevant CSS:
JavaScript:
Basically what's going on is that there's a fixed viewport (
scroller
) with hidden overflow that contains a really longdiv
(items
) containing all the images. Theitems
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/
If you remove 'display:none' from the CSS (on img tags), here is the code that works with your markup and rotates images on right and left buttons: