PageRenderTime 59ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/yii/danyserwis_test/themes/35434 template preview_pliki/jcarousellite_1.js

http://htdocstest.googlecode.com/
JavaScript | 340 lines | 110 code | 27 blank | 203 comment | 25 complexity | d0b5b875068965a48dc716c5b3cc5ffa MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, BSD-2-Clause
  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. speed: 200,
  210. easing: null,
  211. vertical: false,
  212. circular: false,
  213. start: 0,
  214. scroll: 1,
  215. beforeStart: null,
  216. afterEnd: null
  217. }, o || {});
  218. return this.each(function() { // Returns the element collection. Chainable.
  219. var running = false, animCss=o.vertical?"top":"left", sizeCss=o.vertical?"height":"width";
  220. var div = $(this), ul = $("ul", div), tLi = $("li", ul), tl = tLi.size(), v = o.visible;
  221. if(o.circular) {
  222. ul.prepend(tLi.slice(tl-v-1+1).clone())
  223. .append(tLi.slice(0,v).clone());
  224. o.start += v;
  225. }
  226. var li = $("li", ul), itemLength = li.size(), curr = o.start;
  227. div.css("visibility", "visible");
  228. li.css({overflow: "hidden", float: o.vertical ? "none" : "left"});
  229. ul.css({margin: "0", padding: "0", position: "relative", "list-style-type": "none", "z-index": "1"});
  230. div.css({overflow: "hidden", position: "relative", "z-index": "2", left: "0px"});
  231. var liSize = o.vertical ? height(li) : width(li); // Full li size(incl margin)-Used for animation
  232. var ulSize = liSize * itemLength; // size of full ul(total length, not just for the visible items)
  233. var divSize = liSize * v; // size of entire div(total length for just the visible items)
  234. li.css({width: li.width(), height: li.height()});
  235. ul.css(sizeCss, ulSize+"px").css(animCss, -(curr*liSize));
  236. div.css(sizeCss, divSize+"px"); // Width of the DIV. length of visible images
  237. if(o.btnPrev)
  238. $(o.btnPrev).click(function() {
  239. return go(curr-o.scroll);
  240. });
  241. if(o.btnNext)
  242. $(o.btnNext).click(function() {
  243. return go(curr+o.scroll);
  244. });
  245. if(o.btnGo)
  246. $.each(o.btnGo, function(i, val) {
  247. $(val).click(function() {
  248. return go(o.circular ? o.visible+i : i);
  249. });
  250. });
  251. if(o.mouseWheel && div.mousewheel)
  252. div.mousewheel(function(e, d) {
  253. return d>0 ? go(curr-o.scroll) : go(curr+o.scroll);
  254. });
  255. if(o.auto)
  256. setInterval(function() {
  257. go(curr+o.scroll);
  258. }, o.auto+o.speed);
  259. function vis() {
  260. return li.slice(curr).slice(0,v);
  261. };
  262. function go(to) {
  263. if(!running) {
  264. if(o.beforeStart)
  265. o.beforeStart.call(this, vis());
  266. if(o.circular) { // If circular we are in first or last, then goto the other end
  267. if(to<=o.start-v-1) { // If first, then goto last
  268. ul.css(animCss, -((itemLength-(v*2))*liSize)+"px");
  269. // If "scroll" > 1, then the "to" might not be equal to the condition; it can be lesser depending on the number of elements.
  270. curr = to==o.start-v-1 ? itemLength-(v*2)-1 : itemLength-(v*2)-o.scroll;
  271. } else if(to>=itemLength-v+1) { // If last, then goto first
  272. ul.css(animCss, -( (v) * liSize ) + "px" );
  273. // If "scroll" > 1, then the "to" might not be equal to the condition; it can be greater depending on the number of elements.
  274. curr = to==itemLength-v+1 ? v+1 : v+o.scroll;
  275. } else curr = to;
  276. } else { // If non-circular and to points to first or last, we just return.
  277. if(to<0 || to>itemLength-v) return;
  278. else curr = to;
  279. } // If neither overrides it, the curr will still be "to" and we can proceed.
  280. running = true;
  281. ul.animate(
  282. animCss == "left" ? { left: -(curr*liSize) } : { top: -(curr*liSize) } , o.speed, o.easing,
  283. function() {
  284. if(o.afterEnd)
  285. o.afterEnd.call(this, vis());
  286. running = false;
  287. }
  288. );
  289. // Disable buttons when the carousel reaches the last/first, and enable when not
  290. if(!o.circular) {
  291. $(o.btnPrev + "," + o.btnNext).removeClass("disabled");
  292. $( (curr-o.scroll<0 && o.btnPrev)
  293. ||
  294. (curr+o.scroll > itemLength-v && o.btnNext)
  295. ||
  296. []
  297. ).addClass("disabled");
  298. }
  299. }
  300. return false;
  301. };
  302. });
  303. };
  304. function css(el, prop) {
  305. return parseInt($.css(el[0], prop)) || 0;
  306. };
  307. function width(el) {
  308. return el[0].offsetWidth + css(el, 'marginLeft') + css(el, 'marginRight');
  309. };
  310. function height(el) {
  311. return el[0].offsetHeight + css(el, 'marginTop') + css(el, 'marginBottom');
  312. };
  313. })(jQuery);