jquery alternation js -- jQuery plugin for DOM element alternation Cop

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
/*
** jquery.alternation.js -- jQuery plugin for DOM element alternation
** Copyright (c) 2007 Ralf S. Engelschall <rse@engelschall.com>
** Copyright (c) 2006 Torsten Baldes & Matt Oakes
** Licensed under GPL <http://www.gnu.org/licenses/gpl.txt>
**
** $LastChangedDate: 2007-03-18 12:17:31 +0200 (Sun, 18 Mar 2007) $
** $LastChangedRevision: 18 $
*/
/*
* <div class="alternation">
* <div>content 1</div>
* <div>content 2</div>
* <div>content 3</div>
* </div>
*
* $('.alternation').alternation({
* children: optional children selector (default: 'null')
* alternation: alternation type: "sequence" or "random" (default: 'sequence')
* timeout: alternation time (in ms) (default: 2000)
* animation: animation type: "fade" or "slide" (default: "fade")
* speed: animation speed (in ms or "slow", "normal", "fast") (default: "normal")
* class: container element CSS class (default: "alternation")
* height: container element CSS height (default: "auto")
* });
*/
(function($) {
/* jQuery dynamic object method */
$.fn.alternation = function(options) {
return this.each(function() {
$.alternation(this, options);
});
};
/* jQuery static function */
$.alternation = function(container, options) {
var settings = {
'children': null,
'alternation': 'sequence',
'timeout': 2000,
'animation': 'fade',
'speed': 'normal',
'class': 'alternation',
'height': 'auto'
};
if (options)
$.extend(settings, options);
if (settings.children === null)
var elements = $(container).children();
else
var elements = $(container).children(settings.children);
if (elements.length > 1) {
$(container).css('position', 'relative');
$(container).css('height', settings["height"]);
$(container).addClass(settings["class"]);
for (var i = 0; i < elements.length; i++) {
$(elements[i]).css('z-index', String(elements.length-i));
$(elements[i]).css('position', 'absolute');
$(elements[i]).hide();
};
if (settings["alternation"] == "sequence") {
setTimeout(function() {
$.alternation._next(elements, settings, 1, 0);
}, settings.timeout);
$(elements[0]).show();
} else if (settings["alternation"] == "random") {
setTimeout(function() {
do {
current = Math.floor(Math.random() * elements.length);
} while (current == 0)
$.alternation._next(elements, settings, current, 0);
}, settings.timeout);
$(elements[0]).show();
} else
alert('jQuery: alternation: parameter "alternation" must either be "sequence" or "random"');
}
};
/* jQuery static function */
$.alternation._next = function(elements, settings, current, last) {
if (settings["animation"] == 'slide') {
$(elements[last]).slideUp(settings.speed,
$(elements[current]).slideDown(settings.speed));
} else if (settings["animation"] == 'fade') {
$(elements[last]).fadeOut(settings.speed);
$(elements[current]).fadeIn(settings.speed);
} else
alert('jQuery: alternation: parameter "animation" must either be "slide" or "fade"');
if (settings["alternation"] == "sequence") {
if ((current + 1) < elements.length) {
current = current + 1;
last = current - 1;
} else {
current = 0;
last = elements.length - 1;
}
} else if (settings["alternation"] == "random") {
last = current;
while (current == last)
current = Math.floor(Math.random() * elements.length);
} else
alert('jQuery: alternation: parameter "alternation" must either be "sequence" or "random"');
setTimeout((function() {
$.alternation._next(elements, settings, current, last);
}), settings.timeout);
};
})(jQuery);