Created
September 28, 2012 15:16
-
-
Save chrisbreiding/3800459 to your computer and use it in GitHub Desktop.
Simple jQuery Slider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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