|
18 | 18 | $cycles = this.find('.cycle-cycles div'),
|
19 | 19 | max = $cycles.length,
|
20 | 20 | min = 0,
|
| 21 | + current_index = 0, |
21 | 22 | options = $.extend(defaults, options);
|
| 23 | + |
| 24 | + function set_current_cycle(num) { |
| 25 | + current_index = num; |
| 26 | + $cycles.removeClass('current-cycle'); |
| 27 | + $cycles.hide(); |
| 28 | + $($cycles[num]).show(options.delay, options.transition).addClass('current-cycle'); |
22 | 29 |
|
| 30 | + } |
| 31 | + |
23 | 32 | function current_cycle_num() {
|
24 |
| - var rval; |
25 |
| - $cycles.each(function(i, elem) { |
26 |
| - if($(elem).is(':visible')) { |
27 |
| - rval = i; |
28 |
| - return false; // returning false stops each() from within callback |
29 |
| - } |
30 |
| - }); |
31 |
| - return rval; |
| 33 | + return current_index; |
32 | 34 | }
|
33 | 35 | function callback_if_necessary() {
|
34 | 36 | if(typeof options.success === 'function') {
|
|
39 | 41 | var next = function next() {
|
40 | 42 | var cycle_num = current_cycle_num(),
|
41 | 43 | $current = $($cycles[cycle_num]);
|
| 44 | + |
42 | 45 | if(cycle_num < (max - 1)) {
|
43 |
| - $current.hide(options.delay, options.transition, function() { |
44 |
| - $($cycles[cycle_num + 1]).show(options.delay, options.transition); |
45 |
| - }); |
| 46 | + set_current_cycle(cycle_num + 1); |
46 | 47 | callback_if_necessary();
|
47 | 48 | } else {
|
48 | 49 | if(options.loop) {
|
|
52 | 53 | };
|
53 | 54 | var prev = function prev() {
|
54 | 55 | var cycle_num = current_cycle_num(),
|
55 |
| - $current = $($cycles[cycle_num]); |
| 56 | + $current = $($cycles[cycle_num]); |
56 | 57 | if(cycle_num > min) {
|
57 |
| - $current.hide( options.delay, options.transition, function() { |
58 |
| - $($cycles[cycle_num - 1]).show(options.delay, options.transition); |
59 |
| - }); |
60 |
| - callback_if_necessary(); |
| 58 | + set_current_cycle(cycle_num - 1); |
| 59 | + callback_if_necessary(); |
61 | 60 | } else {
|
62 | 61 | if(options.loop) {
|
63 | 62 | end();
|
64 | 63 | }
|
65 | 64 | }
|
66 | 65 | };
|
67 | 66 | var begin = function begin() {
|
68 |
| - var cycle_num = current_cycle_num(), |
69 |
| - $current = $($cycles[cycle_num]); |
70 |
| - $current.hide(options.delay, options.transition, function() { |
71 |
| - $($cycles[0]).show(options.delay, options.transition); |
72 |
| - }); |
73 |
| - callback_if_necessary(); |
| 67 | + set_current_cycle(0); |
| 68 | + callback_if_necessary(); |
74 | 69 | };
|
75 | 70 | var end = function end() {
|
76 |
| - var cycle_num = current_cycle_num(), |
77 |
| - $current = $($cycles[cycle_num]); |
78 |
| - $current.hide(options.delay, options.transition, function() { |
79 |
| - $($cycles[max - 1]).show(options.delay, options.transition); |
80 |
| - }); |
| 71 | + set_current_cycle(max - 1); |
81 | 72 | callback_if_necessary();
|
82 | 73 | };
|
83 | 74 |
|
|
94 | 85 | $end.die().live('click', function() {
|
95 | 86 | end();
|
96 | 87 | });
|
| 88 | + set_current_cycle(0); |
97 | 89 | begin();
|
98 |
| - $cycles.not(":first-child").hide(); |
99 | 90 | }
|
100 | 91 |
|
101 | 92 | init();
|
|
104 | 95 | next : next,
|
105 | 96 | prev : prev,
|
106 | 97 | begin : begin,
|
107 |
| - end : end |
| 98 | + end : end, |
108 | 99 | }
|
109 | 100 | }
|
110 | 101 | });
|
|
0 commit comments