Skip to content

Instantly share code, notes, and snippets.

@chrisbreiding
Created September 28, 2012 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisbreiding/3800459 to your computer and use it in GitHub Desktop.
Save chrisbreiding/3800459 to your computer and use it in GitHub Desktop.
Simple jQuery Slider
<div class="slider">
<!-- items can be any tag (e.g. <a>, <img>, <li>, etc.) -->
<div class="slide"></div>
<div class="slide"></div>
<div class="slide"></div>
</div>
jQuery(function($) {
var speed = 2000,
interval = 5000;
$slides = $('.slide'),
numSlides = $slides.length,
i = 0;
if ( numSlides > 1) {
setInterval(function () {
$slides.eq( i ).fadeOut(speed);
$slides.eq( (i + 1) % numSlides ).fadeIn(speed);
i = i < numSlides - 1 ? i + 1 : 0;
}, interval);
}
});
.slider {
height: 350px;
position: relative;
}
/*
* may need to specify width and height of .slide if their contents are not
* images or something else with a defined width and height
*/
.slide {
display: none;
position: absolute;
}
.slide:first-child {
display: block;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment