jcarousellite.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /**
  2. * jCarouselLite - jQuery plugin to navigate images/any content in a carousel style widget.
  3. * @requires jQuery v1.2 or above
  4. *
  5. * http://gmarwaha.com/jquery/jcarousellite/
  6. *
  7. * Copyright (c) 2007 Ganeshji Marwaha (gmarwaha.com)
  8. * Dual licensed under the MIT and GPL licenses:
  9. * http://www.opensource.org/licenses/mit-license.php
  10. * http://www.gnu.org/licenses/gpl.html
  11. *
  12. * Version: 1.0.1
  13. * Note: Requires jquery 1.2 or above from version 1.0.1
  14. */
  15. /**
  16. * Creates a carousel-style navigation widget for images/any-content from a simple HTML markup.
  17. *
  18. * The HTML markup that is used to build the carousel can be as simple as...
  19. *
  20. * <div class="carousel">
  21. * <ul>
  22. * <li><img src="image/1.jpg" alt="1"></li>
  23. * <li><img src="image/2.jpg" alt="2"></li>
  24. * <li><img src="image/3.jpg" alt="3"></li>
  25. * </ul>
  26. * </div>
  27. *
  28. * As you can see, this snippet is nothing but a simple div containing an unordered list of images.
  29. * You don't need any special "class" attribute, or a special "css" file for this plugin.
  30. * I am using a class attribute just for the sake of explanation here.
  31. *
  32. * To navigate the elements of the carousel, you need some kind of navigation buttons.
  33. * For example, you will need a "previous" button to go backward, and a "next" button to go forward.
  34. * This need not be part of the carousel "div" itself. It can be any element in your page.
  35. * Lets assume that the following elements in your document can be used as next, and prev buttons...
  36. *
  37. * <button class="prev">&lt;&lt;</button>
  38. * <button class="next">&gt;&gt;</button>
  39. *
  40. * Now, all you need to do is call the carousel component on the div element that represents it, and pass in the
  41. * navigation buttons as options.
  42. *
  43. * $(".carousel").jCarouselLite({
  44. * btnNext: ".next",
  45. * btnPrev: ".prev"
  46. * });
  47. *
  48. * That's it, you would have now converted your raw div, into a magnificient carousel.
  49. *
  50. * There are quite a few other options that you can use to customize it though.
  51. * Each will be explained with an example below.
  52. *
  53. * @param an options object - You can specify all the options shown below as an options object param.
  54. *
  55. * @option btnPrev, btnNext : string - no defaults
  56. * @example
  57. * $(".carousel").jCarouselLite({
  58. * btnNext: ".next",
  59. * btnPrev: ".prev"
  60. * });
  61. * @desc Creates a basic carousel. Clicking "btnPrev" navigates backwards and "btnNext" navigates forward.
  62. *
  63. * @option btnGo - array - no defaults
  64. * @example
  65. * $(".carousel").jCarouselLite({
  66. * btnNext: ".next",
  67. * btnPrev: ".prev",
  68. * btnGo: [".0", ".1", ".2"]
  69. * });
  70. * @desc If you don't want next and previous buttons for navigation, instead you prefer custom navigation based on
  71. * the item number within the carousel, you can use this option. Just supply an array of selectors for each element
  72. * in the carousel. The index of the array represents the index of the element. What i mean is, if the
  73. * first element in the array is ".0", it means that when the element represented by ".0" is clicked, the carousel
  74. * will slide to the first element and so on and so forth. This feature is very powerful. For example, i made a tabbed
  75. * interface out of it by making my navigation elements styled like tabs in css. As the carousel is capable of holding
  76. * any content, not just images, you can have a very simple tabbed navigation in minutes without using any other plugin.
  77. * The best part is that, the tab will "slide" based on the provided effect. :-)
  78. *
  79. * @option mouseWheel : boolean - default is false
  80. * @example
  81. * $(".carousel").jCarouselLite({
  82. * mouseWheel: true
  83. * });
  84. * @desc The carousel can also be navigated using the mouse wheel interface of a scroll mouse instead of using buttons.
  85. * To get this feature working, you have to do 2 things. First, you have to include the mouse-wheel plugin from brandon.
  86. * Second, you will have to set the option "mouseWheel" to true. That's it, now you will be able to navigate your carousel
  87. * using the mouse wheel. Using buttons and mouseWheel or not mutually exclusive. You can still have buttons for navigation
  88. * as well. They complement each other. To use both together, just supply the options required for both as shown below.
  89. * @example
  90. * $(".carousel").jCarouselLite({
  91. * btnNext: ".next",
  92. * btnPrev: ".prev",
  93. * mouseWheel: true
  94. * });
  95. *
  96. * @option auto : number - default is null, meaning autoscroll is disabled by default
  97. * @example
  98. * $(".carousel").jCarouselLite({
  99. * auto: 800,
  100. * speed: 500
  101. * });
  102. * @desc You can make your carousel auto-navigate itself by specfying a millisecond value in this option.
  103. * The value you specify is the amount of time between 2 slides. The default is null, and that disables auto scrolling.
  104. * Specify this value and magically your carousel will start auto scrolling.
  105. *
  106. * @option speed : number - 200 is default
  107. * @example
  108. * $(".carousel").jCarouselLite({
  109. * btnNext: ".next",
  110. * btnPrev: ".prev",
  111. * speed: 800
  112. * });
  113. * @desc Specifying a speed will slow-down or speed-up the sliding speed of your carousel. Try it out with
  114. * different speeds like 800, 600, 1500 etc. Providing 0, will remove the slide effect.
  115. *
  116. * @option easing : string - no easing effects by default.
  117. * @example
  118. * $(".carousel").jCarouselLite({
  119. * btnNext: ".next",
  120. * btnPrev: ".prev",
  121. * easing: "bounceout"
  122. * });
  123. * @desc You can specify any easing effect. Note: You need easing plugin for that. Once specified,
  124. * the carousel will slide based on the provided easing effect.
  125. *
  126. * @option vertical : boolean - default is false
  127. * @example
  128. * $(".carousel").jCarouselLite({
  129. * btnNext: ".next",
  130. * btnPrev: ".prev",
  131. * vertical: true
  132. * });
  133. * @desc Determines the direction of the carousel. true, means the carousel will display vertically. The next and
  134. * prev buttons will slide the items vertically as well. The default is false, which means that the carousel will
  135. * display horizontally. The next and prev items will slide the items from left-right in this case.
  136. *
  137. * @option circular : boolean - default is true
  138. * @example
  139. * $(".carousel").jCarouselLite({
  140. * btnNext: ".next",
  141. * btnPrev: ".prev",
  142. * circular: false
  143. * });
  144. * @desc Setting it to true enables circular navigation. This means, if you click "next" after you reach the last
  145. * element, you will automatically slide to the first element and vice versa. If you set circular to false, then
  146. * if you click on the "next" button after you reach the last element, you will stay in the last element itself
  147. * and similarly for "previous" button and first element.
  148. *
  149. * @option visible : number - default is 3
  150. * @example
  151. * $(".carousel").jCarouselLite({
  152. * btnNext: ".next",
  153. * btnPrev: ".prev",
  154. * visible: 4
  155. * });
  156. * @desc This specifies the number of items visible at all times within the carousel. The default is 3.
  157. * You are even free to experiment with real numbers. Eg: "3.5" will have 3 items fully visible and the
  158. * last item half visible. This gives you the effect of showing the user that there are more images to the right.
  159. *
  160. * @option start : number - default is 0
  161. * @example
  162. * $(".carousel").jCarouselLite({
  163. * btnNext: ".next",
  164. * btnPrev: ".prev",
  165. * start: 2
  166. * });
  167. * @desc You can specify from which item the carousel should start. Remember, the first item in the carousel
  168. * has a start of 0, and so on.
  169. *
  170. * @option scrool : number - default is 1
  171. * @example
  172. * $(".carousel").jCarouselLite({
  173. * btnNext: ".next",
  174. * btnPrev: ".prev",
  175. * scroll: 2
  176. * });
  177. * @desc The number of items that should scroll/slide when you click the next/prev navigation buttons. By
  178. * default, only one item is scrolled, but you may set it to any number. Eg: setting it to "2" will scroll
  179. * 2 items when you click the next or previous buttons.
  180. *
  181. * @option beforeStart, afterEnd : function - callbacks
  182. * @example
  183. * $(".carousel").jCarouselLite({
  184. * btnNext: ".next",
  185. * btnPrev: ".prev",
  186. * beforeStart: function(a) {
  187. * alert("Before animation starts:" + a);
  188. * },
  189. * afterEnd: function(a) {
  190. * alert("After animation ends:" + a);
  191. * }
  192. * });
  193. * @desc If you wanted to do some logic in your page before the slide starts and after the slide ends, you can
  194. * register these 2 callbacks. The functions will be passed an argument that represents an array of elements that
  195. * are visible at the time of callback.
  196. *
  197. *
  198. * @cat Plugins/Image Gallery
  199. * @author Ganeshji Marwaha/ganeshread@gmail.com
  200. */
  201. (function($) { // Compliant with jquery.noConflict()
  202. $.fn.jCarouselLite = function(o) {
  203. o = $.extend({
  204. btnPrev: null,
  205. btnNext: null,
  206. btnGo: null,
  207. mouseWheel: false,
  208. auto: null,
  209. hoverPause: false,
  210. speed: 200,
  211. easing: null,
  212. vertical: false,
  213. circular: true,
  214. visible: 3,
  215. start: 0,
  216. scroll: 1,
  217. beforeStart: null,
  218. afterEnd: null
  219. }, o || {});
  220. return this.each(function() { // Returns the element collection. Chainable.
  221. var running = false, animCss=o.vertical?"top":"left", sizeCss=o.vertical?"height":"width";
  222. var div = $(this), ul = $("ul", div), tLi = $("li", ul), tl = tLi.size(), v = o.visible;
  223. if(o.circular) {
  224. ul.prepend(tLi.slice(tl-v+1).clone())
  225. .append(tLi.slice(0,o.scroll).clone());
  226. o.start += v-1;
  227. }
  228. var li = $("li", ul), itemLength = li.size(), curr = o.start;
  229. div.css("visibility", "visible");
  230. li.css({overflow: "hidden", float: o.vertical ? "none" : "left"});
  231. ul.css({margin: "0", padding: "0", position: "relative", "list-style-type": "none", "z-index": "1"});
  232. div.css({overflow: "hidden", position: "relative", "z-index": "2", left: "0px"});
  233. var liSize = o.vertical ? height(li) : width(li); // Full li size(incl margin)-Used for animation
  234. var ulSize = liSize * itemLength; // size of full ul(total length, not just for the visible items)
  235. var divSize = liSize * v; // size of entire div(total length for just the visible items)
  236. li.css({width: li.width(), height: li.height()});
  237. ul.css(sizeCss, ulSize+"px").css(animCss, -(curr*liSize));
  238. div.css(sizeCss, divSize+"px"); // Width of the DIV. length of visible images
  239. if(o.btnPrev) {
  240. $(o.btnPrev).click(function() {
  241. return go(curr-o.scroll);
  242. });
  243. if(o.hoverPause) {
  244. $(o.btnPrev).hover(function(){stopAuto();}, function(){startAuto();});
  245. }
  246. }
  247. if(o.btnNext) {
  248. $(o.btnNext).click(function() {
  249. return go(curr+o.scroll);
  250. });
  251. if(o.hoverPause) {
  252. $(o.btnNext).hover(function(){stopAuto();}, function(){startAuto();});
  253. }
  254. }
  255. if(o.btnGo)
  256. $.each(o.btnGo, function(i, val) {
  257. $(val).click(function() {
  258. return go(o.circular ? o.visible+i : i);
  259. });
  260. });
  261. if(o.mouseWheel && div.mousewheel)
  262. div.mousewheel(function(e, d) {
  263. return d>0 ? go(curr-o.scroll) : go(curr+o.scroll);
  264. });
  265. var autoInterval;
  266. function startAuto() {
  267. stopAuto();
  268. autoInterval = setInterval(function() {
  269. go(curr+o.scroll);
  270. }, o.auto+o.speed);
  271. };
  272. function stopAuto() {
  273. clearInterval(autoInterval);
  274. };
  275. if(o.auto) {
  276. if(o.hoverPause) {
  277. div.hover(function(){stopAuto();}, function(){startAuto();});
  278. }
  279. startAuto();
  280. };
  281. function vis() {
  282. return li.slice(curr).slice(0,v);
  283. };
  284. function go(to) {
  285. if(!running) {
  286. if(o.beforeStart)
  287. o.beforeStart.call(this, vis());
  288. if(o.circular) { // If circular we are in first or last, then goto the other end
  289. if(to<0) { // If before range, then go around
  290. ul.css(animCss, -( (curr + tl) * liSize)+"px");
  291. curr = to + tl;
  292. } else if(to>itemLength-v) { // If beyond range, then come around
  293. ul.css(animCss, -( (curr - tl) * liSize ) + "px" );
  294. curr = to - tl;
  295. } else curr = to;
  296. } else { // If non-circular and to points to first or last, we just return.
  297. if(to<0 || to>itemLength-v) return;
  298. else curr = to;
  299. } // If neither overrides it, the curr will still be "to" and we can proceed.
  300. running = true;
  301. ul.animate(
  302. animCss == "left" ? { left: -(curr*liSize) } : { top: -(curr*liSize) } , o.speed, o.easing,
  303. function() {
  304. if(o.afterEnd)
  305. o.afterEnd.call(this, vis());
  306. running = false;
  307. }
  308. );
  309. // Disable buttons when the carousel reaches the last/first, and enable when not
  310. if(!o.circular) {
  311. $(o.btnPrev + "," + o.btnNext).removeClass("disabled");
  312. $( (curr-o.scroll<0 && o.btnPrev)
  313. ||
  314. (curr+o.scroll > itemLength-v && o.btnNext)
  315. ||
  316. []
  317. ).addClass("disabled");
  318. }
  319. }
  320. return false;
  321. };
  322. });
  323. };
  324. function css(el, prop) {
  325. return parseInt($.css(el[0], prop)) || 0;
  326. };
  327. function width(el) {
  328. return el[0].offsetWidth + css(el, 'marginLeft') + css(el, 'marginRight');
  329. };
  330. function height(el) {
  331. return el[0].offsetHeight + css(el, 'marginTop') + css(el, 'marginBottom');
  332. };
  333. })(jQuery);