Last active
January 3, 2020 20:29
-
-
Save jennschiffer/5678348 to your computer and use it in GitHub Desktop.
simple stupid jquery carousel
This file contains 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
#my-carousel-of-dreams { position: relative; width: 100%; max-width: 950px; overflow: hidden; } | |
ul.slidescroller { margin: 0; padding: 0 20px; position: relative; width: 9999px; } | |
ul.slidescroller > li { width: 280px; float: left; padding: 10px; margin: 5px; overflow: hidden; } | |
ul.slidescroller li { list-style: none; } | |
#my-carousel-of-dreams .next, #my-carousel-of-dreams .prev { /* positioning for arrows here */ } |
This file contains 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 id="my-carousel-of-dreams"> | |
<span class="previous">←</span> | |
<span class="next">→</span> | |
<ul class="carousel"> | |
<li>First Slide</li> | |
<li>Second Slide</li> | |
<li>Third Slide</li> | |
</ul> | |
</div> |
This file contains 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
$(function(){ | |
var visibleItems = 3; | |
var $carousel = $("ul.carousel"); | |
var $slideItems = $("ul.carousel > li"); | |
var $firstItem; | |
var setCurrentItems = function() { | |
$slideItems.hide(); | |
$('.current').removeClass('current'); | |
$currentItems = $("ul.carousel > li:lt(" + visibleItems + ")"); | |
$currentItems.addClass('current').show(); | |
} | |
var nextItem = function() { | |
$firstItem = $("ul.carousel > li:first-child"); | |
$carousel.append($firstItem); | |
setCurrentItems(); | |
} | |
var previousItem = function() { | |
$lastItem = $("ul.carousel > li:last-child"); | |
$carousel.prepend($lastItem); | |
setCurrentItems(); | |
} | |
$("ul.carousel").siblings(".next").click(nextItem); | |
$("ul.carousel").siblings(".previous").click(previousItem); | |
setCurrentItems(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you! But how can I make it so only the active slide is visible?