jcarousellite.js 15 KB

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