I am struggling to figure out how to create a looping effect for the images inside the slider. I would like the track to seamlessly loop around from both sides after they are hidden. Here is the link to the current jsfiddle: http://jsfiddle.net/V2x6s/3/
Below is the JavaScript code:
setInterval(function() {
var left1 = parseInt($('#track1').css('left')),
left2 = parseInt($('#track2').css('left')),
left3 = parseInt($('#track3').css('left'));
if ($('#left1').is(":hover")) {
$('#track1').css('left', left1+2);
}
else if ($('#left2').is(":hover")) {
$('#track2').css('left', left2+2);
}
else if ($('#left3').is(":hover")) {
$('#track3').css('left', left3+2);
}
else if ($('#right1').is(":hover")) {
$('#track1').css('left', left1-2);
}
else if ($('#right2').is(":hover")) {
$('#track2').css('left', left2-2);
}
else if ($('#right3').is(":hover")) {
$('#track3').css('left', left3-2);
}
}, 10);
.slider {
position: relative;
background-color: #ccc;
height: 150px;
margin: 10px;
padding: 10px;
overflow: hidden;
}
.track {
position: absolute;
top: 10px;
left: 10px;
margin: 0;
padding: 0;
border: 0;
width: 2000px;
}
.book {
float: left;
margin: 0;
margin-right: 10px;
padding: 0;
border: 0;
width: 150px;
height: 150px;
-webkit-transition: opacity 0.2s;
-moz-transition: opacity 0.2s;
-ms-transition: opacity 0.2s;
-o-transition: opacity 0.2s;
-webkit-transition: opacity 0.2s;
}
.book:hover {
opacity: 0.5;
}
.book:nth-child(1) {
background-color: #ff0000;
}
.book:nth-child(2) {
background-color: #ffaa00;
}
.book:nth-child(3) {
background-color: #ffff00;
}
.book:nth-child(4) {
background-color: #00ff00;
}
.book:nth-child(5) {
background-color: #0000ff;
}
.book:nth-child(6) {
background-color: #aa00ff;
}
.book:nth-child(7) {
background-color: #ff00ff;
}
.left, .right {
position: absolute;
color: white;
font-size: 48px;
top: 57px;
cursor: pointer;
z-index: 1;
}
.left {
left: 0;
}
.right {
right: 0;
}
<div class="slider">
<div id="left1" class="left"><</div>
<div id="right1" class="right">></div>
<div class="track" id="track1">
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
</div>
</div>
<div class="slider">
<div id="left2" class="left"><</div>
<div id="right2" class="right">></div>
<div class="track" id="track2">
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
</div>
</div>
<div class="slider">
<div id="left3" class="left"><</div>
<div id="right3" class="right">></div>
<div class="track" id="track3">
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
<div class="book">
</div>
</div>
</div>