-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.rotator.js
executable file
·72 lines (62 loc) · 2.23 KB
/
jquery.rotator.js
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
/*global window, setTimeout, jQuery */
(function ($) {
/* Banner rotator plugin for jQuery
* Version 0.2.3
*
* Copyright (c) 2011 Luminosity Group
*/
$.fn.rotator = function (options) {
var container, defaults, stopped, settings, size, i;
container = this;
defaults = {
interval: 4000,
randomize: false,
fadeInTime: 500,
fadeOutTime: 1000,
stopOnBlur: false
};
/* Used when settings.stopOnBlur == true */
stopped = false;
/* Merge options with defaults */
settings = $.extend(true, {}, defaults, options);
/* First, hide all the elements */
$('li', container).each(function () {
$(this).hide();
});
/* If randomize is set, pick an element randomly and show it, else show
* the first element
*/
if (settings.randomize) {
size = $('li', container).length;
i = Math.floor(Math.random() * size) + 1;
$('li:nth-child(' + i + ')', container).show().addClass('active');
} else {
$('li', container).first().show().addClass('active');
}
if (settings.stopOnBlur) {
$(window).blur(function () {
stopped = true;
}).focus(function () {
stopped = false;
});
}
/* Does the fadein/fadeout to the next item in the list */
function rotate() {
var current;
if (!settings.stopOnBlur || (settings.stopOnBlur && !stopped)) {
current = $('li.active', container);
$('li', container).removeClass('active');
if ($(current).next().length > 0) {
$(current).fadeOut(settings.fadeInTime)
.next().fadeIn(settings.fadeOutTime).addClass('active');
}
else {
$(current).fadeOut(settings.fadetime);
$('li:first-child', container).fadeIn(settings.fadeInTime).addClass('active');
}
}
setTimeout(rotate, settings.interval);
}
setTimeout(rotate, settings.interval);
};
}(jQuery));