Skip to content

Instantly share code, notes, and snippets.

@arvindang
Created October 21, 2013 16:42
Show Gist options
  • Save arvindang/7086902 to your computer and use it in GitHub Desktop.
Save arvindang/7086902 to your computer and use it in GitHub Desktop.
jQuery slideshow with CSS transition.
/* Base */
.group:after {
content: "";
display: table;
clear: both;
}
/* Slideshow */
.slideshow {
width: 500px;
margin: 50px auto;
text-align: center;
overflow: hidden;
}
.slides {
position: relative;
list-style: none;
width: 300%;
padding: 0;
-webkit-transition: left .2s ease-out;
}
.slides li {
float: left;
width: 500px;
}
nav a {
color: #C30;
text-decoration: none;
}
nav a:hover {
text-decoration: underline;
}
.prev {
float: left;
}
.next {
float: right;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Slideshow</title>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/normalize/2.1.3/normalize.min.css">
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<div class="slideshow">
<h1>Slideshow</h1>
<ul class="slides group">
<li><img src="http://placekitten.com/g/500/300" alt="Kitten #1"></li>
<li><img src="http://placekitten.com/g/500/301" alt="Kitten #2"></li>
<li><img src="http://placekitten.com/g/500/302" alt="Kitten #3"></li>
</ul>
<nav>
<a href="#" class="prev">&laquo; Previous</a>
<a href="#" class="next">Next &raquo;</a>
</nav>
</div>
<script src="http://code.jquery.com/jquery.js"></script>
</body>
</html>
var index = 0;
var title = $("h1");
var slideshow = $(".slides");
var slides = slideshow.find("li");
var slideNum = slides.length;
$("a").click(function () {
var isPrev = $(this).hasClass("prev");
var isNext = $(this).hasClass("next");
if(isPrev){
if (index > 0){
index--;
}
} else {
if(isNext && index < slideNum -1)
index++;
}
title.text("The index is " + index);
slideshow.css("left", (index * -100) + "%");
return false;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment