/bin/html/javascripts/jquery.cycle.lite.js

https://bitbucket.org/VirtualReality/software-testing · JavaScript · 232 lines · 186 code · 30 blank · 16 comment · 57 complexity · 867e99d3b99e34e1163fced0c75586c2 MD5 · raw file

  1. /*!
  2. * jQuery Cycle Lite Plugin
  3. * http://malsup.com/jquery/cycle/lite/
  4. * Copyright (c) 2008-2012 M. Alsup
  5. * Version: 1.6 (02-MAY-2012)
  6. * Dual licensed under the MIT and GPL licenses:
  7. * http://www.opensource.org/licenses/mit-license.php
  8. * http://www.gnu.org/licenses/gpl.html
  9. * Requires: jQuery v1.3.2 or later
  10. */
  11. ;(function($) {
  12. "use strict";
  13. var ver = 'Lite-1.6';
  14. $.fn.cycle = function(options) {
  15. return this.each(function() {
  16. options = options || {};
  17. if (this.cycleTimeout) clearTimeout(this.cycleTimeout);
  18. this.cycleTimeout = 0;
  19. this.cyclePause = 0;
  20. var $cont = $(this);
  21. var $slides = options.slideExpr ? $(options.slideExpr, this) : $cont.children();
  22. var els = $slides.get();
  23. if (els.length < 2) {
  24. if (window.console)
  25. console.log('terminating; too few slides: ' + els.length);
  26. return; // don't bother
  27. }
  28. // support metadata plugin (v1.0 and v2.0)
  29. var opts = $.extend({}, $.fn.cycle.defaults, options || {}, $.metadata ? $cont.metadata() : $.meta ? $cont.data() : {});
  30. var meta = $.isFunction($cont.data) ? $cont.data(opts.metaAttr) : null;
  31. if (meta)
  32. opts = $.extend(opts, meta);
  33. opts.before = opts.before ? [opts.before] : [];
  34. opts.after = opts.after ? [opts.after] : [];
  35. opts.after.unshift(function(){ opts.busy=0; });
  36. // allow shorthand overrides of width, height and timeout
  37. var cls = this.className;
  38. opts.width = parseInt((cls.match(/w:(\d+)/)||[])[1], 10) || opts.width;
  39. opts.height = parseInt((cls.match(/h:(\d+)/)||[])[1], 10) || opts.height;
  40. opts.timeout = parseInt((cls.match(/t:(\d+)/)||[])[1], 10) || opts.timeout;
  41. if ($cont.css('position') == 'static')
  42. $cont.css('position', 'relative');
  43. if (opts.width)
  44. $cont.width(opts.width);
  45. if (opts.height && opts.height != 'auto')
  46. $cont.height(opts.height);
  47. var first = 0;
  48. $slides.css({position: 'absolute', top:0}).each(function(i) {
  49. $(this).css('z-index', els.length-i);
  50. });
  51. $(els[first]).css('opacity',1).show(); // opacity bit needed to handle reinit case
  52. if ($.browser.msie) els[first].style.removeAttribute('filter');
  53. if (opts.fit && opts.width)
  54. $slides.width(opts.width);
  55. if (opts.fit && opts.height && opts.height != 'auto')
  56. $slides.height(opts.height);
  57. if (opts.pause)
  58. $cont.hover(function(){this.cyclePause=1;}, function(){this.cyclePause=0;});
  59. var txFn = $.fn.cycle.transitions[opts.fx];
  60. if (txFn)
  61. txFn($cont, $slides, opts);
  62. $slides.each(function() {
  63. var $el = $(this);
  64. this.cycleH = (opts.fit && opts.height) ? opts.height : $el.height();
  65. this.cycleW = (opts.fit && opts.width) ? opts.width : $el.width();
  66. });
  67. if (opts.cssFirst)
  68. $($slides[first]).css(opts.cssFirst);
  69. if (opts.timeout) {
  70. // ensure that timeout and speed settings are sane
  71. if (opts.speed.constructor == String)
  72. opts.speed = {slow: 600, fast: 200}[opts.speed] || 400;
  73. if (!opts.sync)
  74. opts.speed = opts.speed / 2;
  75. while((opts.timeout - opts.speed) < 250)
  76. opts.timeout += opts.speed;
  77. }
  78. opts.speedIn = opts.speed;
  79. opts.speedOut = opts.speed;
  80. opts.slideCount = els.length;
  81. opts.currSlide = first;
  82. opts.nextSlide = 1;
  83. // fire artificial events
  84. var e0 = $slides[first];
  85. if (opts.before.length)
  86. opts.before[0].apply(e0, [e0, e0, opts, true]);
  87. if (opts.after.length > 1)
  88. opts.after[1].apply(e0, [e0, e0, opts, true]);
  89. if (opts.click && !opts.next)
  90. opts.next = opts.click;
  91. if (opts.next)
  92. $(opts.next).unbind('click.cycle').bind('click.cycle', function(){return advance(els,opts,opts.rev?-1:1);});
  93. if (opts.prev)
  94. $(opts.prev).unbind('click.cycle').bind('click.cycle', function(){return advance(els,opts,opts.rev?1:-1);});
  95. if (opts.timeout)
  96. this.cycleTimeout = setTimeout(function() {
  97. go(els,opts,0,!opts.rev);
  98. }, opts.timeout + (opts.delay||0));
  99. });
  100. };
  101. function go(els, opts, manual, fwd) {
  102. if (opts.busy)
  103. return;
  104. var p = els[0].parentNode, curr = els[opts.currSlide], next = els[opts.nextSlide];
  105. if (p.cycleTimeout === 0 && !manual)
  106. return;
  107. if (manual || !p.cyclePause) {
  108. if (opts.before.length)
  109. $.each(opts.before, function(i,o) { o.apply(next, [curr, next, opts, fwd]); });
  110. var after = function() {
  111. if ($.browser.msie)
  112. this.style.removeAttribute('filter');
  113. $.each(opts.after, function(i,o) { o.apply(next, [curr, next, opts, fwd]); });
  114. queueNext(opts);
  115. };
  116. if (opts.nextSlide != opts.currSlide) {
  117. opts.busy = 1;
  118. $.fn.cycle.custom(curr, next, opts, after);
  119. }
  120. var roll = (opts.nextSlide + 1) == els.length;
  121. opts.nextSlide = roll ? 0 : opts.nextSlide+1;
  122. opts.currSlide = roll ? els.length-1 : opts.nextSlide-1;
  123. } else {
  124. queueNext(opts);
  125. }
  126. function queueNext(opts) {
  127. if (opts.timeout)
  128. p.cycleTimeout = setTimeout(function() { go(els,opts,0,!opts.rev); }, opts.timeout);
  129. }
  130. }
  131. // advance slide forward or back
  132. function advance(els, opts, val) {
  133. var p = els[0].parentNode, timeout = p.cycleTimeout;
  134. if (timeout) {
  135. clearTimeout(timeout);
  136. p.cycleTimeout = 0;
  137. }
  138. opts.nextSlide = opts.currSlide + val;
  139. if (opts.nextSlide < 0) {
  140. opts.nextSlide = els.length - 1;
  141. }
  142. else if (opts.nextSlide >= els.length) {
  143. opts.nextSlide = 0;
  144. }
  145. go(els, opts, 1, val>=0);
  146. return false;
  147. }
  148. $.fn.cycle.custom = function(curr, next, opts, cb) {
  149. var $l = $(curr), $n = $(next);
  150. $n.css(opts.cssBefore);
  151. var fn = function() {$n.animate(opts.animIn, opts.speedIn, opts.easeIn, cb);};
  152. $l.animate(opts.animOut, opts.speedOut, opts.easeOut, function() {
  153. $l.css(opts.cssAfter);
  154. if (!opts.sync)
  155. fn();
  156. });
  157. if (opts.sync)
  158. fn();
  159. };
  160. $.fn.cycle.transitions = {
  161. fade: function($cont, $slides, opts) {
  162. $slides.not(':eq(0)').hide();
  163. opts.cssBefore = { opacity: 0, display: 'block' };
  164. opts.cssAfter = { display: 'none' };
  165. opts.animOut = { opacity: 0 };
  166. opts.animIn = { opacity: 1 };
  167. },
  168. fadeout: function($cont, $slides, opts) {
  169. opts.before.push(function(curr,next,opts,fwd) {
  170. $(curr).css('zIndex',opts.slideCount + (fwd === true ? 1 : 0));
  171. $(next).css('zIndex',opts.slideCount + (fwd === true ? 0 : 1));
  172. });
  173. $slides.not(':eq(0)').hide();
  174. opts.cssBefore = { opacity: 1, display: 'block', zIndex: 1 };
  175. opts.cssAfter = { display: 'none', zIndex: 0 };
  176. opts.animOut = { opacity: 0 };
  177. opts.animIn = { opacity: 1 };
  178. }
  179. };
  180. $.fn.cycle.ver = function() { return ver; };
  181. // @see: http://malsup.com/jquery/cycle/lite/
  182. $.fn.cycle.defaults = {
  183. animIn: {},
  184. animOut: {},
  185. fx: 'fade',
  186. after: null,
  187. before: null,
  188. cssBefore: {},
  189. cssAfter: {},
  190. delay: 0,
  191. fit: 0,
  192. height: 'auto',
  193. metaAttr: 'cycle',
  194. next: null,
  195. pause: false,
  196. prev: null,
  197. speed: 1000,
  198. slideExpr: null,
  199. sync: true,
  200. timeout: 4000
  201. };
  202. })(jQuery);