Skip to content

Instantly share code, notes, and snippets.

@digiguru
Created March 21, 2011 09:32
Show Gist options
  • Save digiguru/879218 to your computer and use it in GitHub Desktop.
Save digiguru/879218 to your computer and use it in GitHub Desktop.
A very very lightweight carousel for jQuery, using the simplest "slide" effect. Can be used on any group of HTML elements (images or text)
(function ($) {
$.fn.dylanCarousel = function (options) {
var settings = $.extend({
circular: true,
pageNo: 0,
item: "li",
button: {
next: ".nextBtn",
prev: ".prevBtn"
}
}, options);
function showPage() {
settings = $(this).data("dc-conf");
$(this).find(settings.item).slideUp();
$($(this).find(settings.item)[settings.pageNo]).slideDown();
}
function clickNext() {
$this = $($(this).data("area"));
$this.data("dc-conf").pageNo++;
if ($this.data("dc-conf").pageNo == $this.find(settings.item).length) {
$this.data("dc-conf").pageNo = 0;
}
$this.trigger("showPage");
}
function clickBack() {
$this = $($(this).data("area"));
$this.data("dc-conf").pageNo--;
if ($this.data("dc-conf").pageNo == -1) {
$this.data("dc-conf").pageNo = $this.find(settings.item).length - 1;
}
$this.trigger("showPage");
}
this.each(function () {
var $this = $(this);
$this.data("dc-conf", settings);
$this.bind("showPage", showPage);
if ($this.find(settings.item).length <= 1) {
$(settings.button.next + "," + settings.button.prev).hide();
}
$(this).find(settings.item).hide();
$(settings.button.next).bind("click", clickNext).data("area", $this);
$(settings.button.prev).bind("click", clickBack).data("area", $this);
$this.trigger("showPage");
});
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment