/**
* jcarousellite - jquery plugin to navigate images/any content in a carousel style widget.
* @requires jquery v1.2 or above
*
* http://gmarwaha.com/jquery/jcarousellite/
*
* copyright (c) 2007 ganeshji marwaha (gmarwaha.com)
* dual licensed under the mit and gpl licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* version: 1.0.1
* note: requires jquery 1.2 or above from version 1.0.1
*/
/**
* creates a carousel-style navigation widget for images/any-content from a simple html markup.
*
* the html markup that is used to build the carousel can be as simple as...
*
*
*
*
*
*
*
*
*
* as you can see, this snippet is nothing but a simple div containing an unordered list of images.
* you don't need any special "class" attribute, or a special "css" file for this plugin.
* i am using a class attribute just for the sake of explanation here.
*
* to navigate the elements of the carousel, you need some kind of navigation buttons.
* for example, you will need a "previous" button to go backward, and a "next" button to go forward.
* this need not be part of the carousel "div" itself. it can be any element in your page.
* lets assume that the following elements in your document can be used as next, and prev buttons...
*
*
*
*
* now, all you need to do is call the carousel component on the div element that represents it, and pass in the
* navigation buttons as options.
*
* $(".carousel").jcarousellite({
* btnnext: ".next",
* btnprev: ".prev"
* });
*
* that's it, you would have now converted your raw div, into a magnificient carousel.
*
* there are quite a few other options that you can use to customize it though.
* each will be explained with an example below.
*
* @param an options object - you can specify all the options shown below as an options object param.
*
* @option btnprev, btnnext : string - no defaults
* @example
* $(".carousel").jcarousellite({
* btnnext: ".next",
* btnprev: ".prev"
* });
* @desc creates a basic carousel. clicking "btnprev" navigates backwards and "btnnext" navigates forward.
*
* @option btngo - array - no defaults
* @example
* $(".carousel").jcarousellite({
* btnnext: ".next",
* btnprev: ".prev",
* btngo: [".0", ".1", ".2"]
* });
* @desc if you don't want next and previous buttons for navigation, instead you prefer custom navigation based on
* the item number within the carousel, you can use this option. just supply an array of selectors for each element
* in the carousel. the index of the array represents the index of the element. what i mean is, if the
* first element in the array is ".0", it means that when the element represented by ".0" is clicked, the carousel
* will slide to the first element and so on and so forth. this feature is very powerful. for example, i made a tabbed
* interface out of it by making my navigation elements styled like tabs in css. as the carousel is capable of holding
* any content, not just images, you can have a very simple tabbed navigation in minutes without using any other plugin.
* the best part is that, the tab will "slide" based on the provided effect. :-)
*
* @option mousewheel : boolean - default is false
* @example
* $(".carousel").jcarousellite({
* mousewheel: true
* });
* @desc the carousel can also be navigated using the mouse wheel interface of a scroll mouse instead of using buttons.
* to get this feature working, you have to do 2 things. first, you have to include the mouse-wheel plugin from brandon.
* second, you will have to set the option "mousewheel" to true. that's it, now you will be able to navigate your carousel
* using the mouse wheel. using buttons and mousewheel or not mutually exclusive. you can still have buttons for navigation
* as well. they complement each other. to use both together, just supply the options required for both as shown below.
* @example
* $(".carousel").jcarousellite({
* btnnext: ".next",
* btnprev: ".prev",
* mousewheel: true
* });
*
* @option auto : number - default is null, meaning autoscroll is disabled by default
* @example
* $(".carousel").jcarousellite({
* auto: 800,
* speed: 500
* });
* @desc you can make your carousel auto-navigate itself by specfying a millisecond value in this option.
* the value you specify is the amount of time between 2 slides. the default is null, and that disables auto scrolling.
* specify this value and magically your carousel will start auto scrolling.
*
* @option speed : number - 200 is default
* @example
* $(".carousel").jcarousellite({
* btnnext: ".next",
* btnprev: ".prev",
* speed: 800
* });
* @desc specifying a speed will slow-down or speed-up the sliding speed of your carousel. try it out with
* different speeds like 800, 600, 1500 etc. providing 0, will remove the slide effect.
*
* @option easing : string - no easing effects by default.
* @example
* $(".carousel").jcarousellite({
* btnnext: ".next",
* btnprev: ".prev",
* easing: "bounceout"
* });
* @desc you can specify any easing effect. note: you need easing plugin for that. once specified,
* the carousel will slide based on the provided easing effect.
*
* @option vertical : boolean - default is false
* @example
* $(".carousel").jcarousellite({
* btnnext: ".next",
* btnprev: ".prev",
* vertical: true
* });
* @desc determines the direction of the carousel. true, means the carousel will display vertically. the next and
* prev buttons will slide the items vertically as well. the default is false, which means that the carousel will
* display horizontally. the next and prev items will slide the items from left-right in this case.
*
* @option circular : boolean - default is true
* @example
* $(".carousel").jcarousellite({
* btnnext: ".next",
* btnprev: ".prev",
* circular: false
* });
* @desc setting it to true enables circular navigation. this means, if you click "next" after you reach the last
* element, you will automatically slide to the first element and vice versa. if you set circular to false, then
* if you click on the "next" button after you reach the last element, you will stay in the last element itself
* and similarly for "previous" button and first element.
*
* @option visible : number - default is 3
* @example
* $(".carousel").jcarousellite({
* btnnext: ".next",
* btnprev: ".prev",
* visible: 4
* });
* @desc this specifies the number of items visible at all times within the carousel. the default is 3.
* you are even free to experiment with real numbers. eg: "3.5" will have 3 items fully visible and the
* last item half visible. this gives you the effect of showing the user that there are more images to the right.
*
* @option start : number - default is 0
* @example
* $(".carousel").jcarousellite({
* btnnext: ".next",
* btnprev: ".prev",
* start: 2
* });
* @desc you can specify from which item the carousel should start. remember, the first item in the carousel
* has a start of 0, and so on.
*
* @option scrool : number - default is 1
* @example
* $(".carousel").jcarousellite({
* btnnext: ".next",
* btnprev: ".prev",
* scroll: 2
* });
* @desc the number of items that should scroll/slide when you click the next/prev navigation buttons. by
* default, only one item is scrolled, but you may set it to any number. eg: setting it to "2" will scroll
* 2 items when you click the next or previous buttons.
*
* @option beforestart, afterend : function - callbacks
* @example
* $(".carousel").jcarousellite({
* btnnext: ".next",
* btnprev: ".prev",
* beforestart: function(a) {
* alert("before animation starts:" + a);
* },
* afterend: function(a) {
* alert("after animation ends:" + a);
* }
* });
* @desc if you wanted to do some logic in your page before the slide starts and after the slide ends, you can
* register these 2 callbacks. the functions will be passed an argument that represents an array of elements that
* are visible at the time of callback.
*
*
* @cat plugins/image gallery
* @author ganeshji marwaha/ganeshread@gmail.com
*/
(function($) { // compliant with jquery.noconflict()
$.fn.jcarousellite = function(o) {
o = $.extend({
btnprev: null,
btnnext: null,
btngo: null,
mousewheel: false,
auto: null,
speed: 200,
easing: null,
vertical: false,
circular: true,
visible: 3,
start: 0,
scroll: 1,
beforestart: null,
afterend: null
}, o || {});
return this.each(function() { // returns the element collection. chainable.
var running = false, animcss=o.vertical?"top":"left", sizecss=o.vertical?"height":"width";
var div = $(this), ul = $("ul", div), tli = $("li", ul), tl = tli.size(), v = o.visible;
if(o.circular) {
ul.prepend(tli.slice(tl-v-1+1).clone())
.append(tli.slice(0,v).clone());
o.start += v;
}
var li = $("li", ul), itemlength = li.size(), curr = o.start;
div.css("visibility", "visible");
li.css({overflow: "hidden", float: o.vertical ? "none" : "left"});
ul.css({margin: "0", padding: "0", position: "relative", "list-style-type": "none", "z-index": "1"});
div.css({overflow: "hidden", position: "relative", "z-index": "2", left: "0px"});
var lisize = o.vertical ? height(li) : width(li); // full li size(incl margin)-used for animation
var ulsize = lisize * itemlength; // size of full ul(total length, not just for the visible items)
var divsize = lisize * v; // size of entire div(total length for just the visible items)
li.css({width: li.width(), height: li.height()});
ul.css(sizecss, ulsize+"px").css(animcss, -(curr*lisize));
div.css(sizecss, divsize+"px"); // width of the div. length of visible images
if(o.btnprev)
$(o.btnprev).click(function() {
return go(curr-o.scroll);
});
if(o.btnnext)
$(o.btnnext).click(function() {
return go(curr+o.scroll);
});
if(o.btngo)
$.each(o.btngo, function(i, val) {
$(val).click(function() {
return go(o.circular ? o.visible+i : i);
});
});
if(o.mousewheel && div.mousewheel)
div.mousewheel(function(e, d) {
return d>0 ? go(curr-o.scroll) : go(curr+o.scroll);
});
if(o.auto)
setinterval(function() {
go(curr+o.scroll);
}, o.auto+o.speed);
function vis() {
return li.slice(curr).slice(0,v);
};
function go(to) {
if(!running) {
if(o.beforestart)
o.beforestart.call(this, vis());
if(o.circular) { // if circular we are in first or last, then goto the other end
if(to<=o.start-v-1) { // if first, then goto last
ul.css(animcss, -((itemlength-(v*2))*lisize)+"px");
// if "scroll" > 1, then the "to" might not be equal to the condition; it can be lesser depending on the number of elements.
curr = to==o.start-v-1 ? itemlength-(v*2)-1 : itemlength-(v*2)-o.scroll;
} else if(to>=itemlength-v+1) { // if last, then goto first
ul.css(animcss, -( (v) * lisize ) + "px" );
// if "scroll" > 1, then the "to" might not be equal to the condition; it can be greater depending on the number of elements.
curr = to==itemlength-v+1 ? v+1 : v+o.scroll;
} else curr = to;
} else { // if non-circular and to points to first or last, we just return.
if(to<0 || to>itemlength-v) return;
else curr = to;
} // if neither overrides it, the curr will still be "to" and we can proceed.
running = true;
ul.animate(
animcss == "left" ? { left: -(curr*lisize) } : { top: -(curr*lisize) } , o.speed, o.easing,
function() {
if(o.afterend)
o.afterend.call(this, vis());
running = false;
}
);
// disable buttons when the carousel reaches the last/first, and enable when not
if(!o.circular) {
$(o.btnprev + "," + o.btnnext).removeclass("disabled");
$( (curr-o.scroll<0 && o.btnprev)
||
(curr+o.scroll > itemlength-v && o.btnnext)
||
[]
).addclass("disabled");
}
}
return false;
};
});
};
function css(el, prop) {
return parseint($.css(el[0], prop)) || 0;
};
function width(el) {
return el[0].offsetwidth + css(el, 'marginleft') + css(el, 'marginright');
};
function height(el) {
return el[0].offsetheight + css(el, 'margintop') + css(el, 'marginbottom');
};
})(jquery);