Edited to scroll images in a view:
This requires some changes in your layout, but will allow you to scroll images 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>
Matching 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 happens is that there is a fixed viewport ( scroller ) with hidden overflow that contains a very long div ( items ) containing all the images. items div just animates every time the next / previous buttons are clicked.
The tricky part moves the last element to the front and vice versa / vice versa when the end of the image line is reached. This is determined by hard-coded values, but can probably be improved using accurate calculation.
Working example: http://jsfiddle.net/andrewwhitaker/6TLK2/3/
source share