forked from rmurphey/jqfundamentals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslideshow-plugin.js
100 lines (71 loc) · 2.23 KB
/
slideshow-plugin.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
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
(function($) {
$.fn.slideshow = function(opts) {
var settings = $.extend({}, $.fn.slideshow.defaults, opts);
return this.each(function() {
var timeout, manualMode = false,
$slideshow = $(this),
$counter = $('<div/>').insertAfter($slideshow),
$controls = $('<div/>').insertAfter($slideshow),
$prevBtn = $('<input/>', {
type : 'button',
value : settings.prevButtonText
}).appendTo($controls),
$nextBtn = $('<input/>', {
type : 'button',
value : settings.nextButtonText
}).appendTo($controls),
$items = $slideshow.find('li').hide(),
total = $items.length,
updateCounter = function(num) {
$counter.text(num + ' of ' + total);
},
getNextItem = function($item) {
return $item.next().length ?
$item.next() : $items.first();
},
getPrevItem = function($item) {
return $item.prev().length ?
$item.prev() : $items.last();
},
showItem = function($currentItem, $itemToShow) {
var $itemToShow =
$itemToShow || getNextItem($currentItem);
$currentItem.fadeOut(settings.fadeDuration, function() {
$itemToShow.fadeIn(settings.fadeDuration, fadeCallback);
});
},
fadeCallback = function() {
if (manualMode) { return; }
var $this = $(this),
$next = getNextItem($this),
num = $this.prevAll().length + 1;
// update the counter
updateCounter(num);
// set the timeout for showing
// the next item in 5 seconds
timeout = setTimeout(function() {
showItem($this, $next);
}, settings.interval);
},
handleBtnClick = function(e) {
clearTimeout(timeout);
manualMode = true;
var $currentItem = $items.filter(':visible'),
$itemToShow = e.data.prev ?
getPrevItem($currentItem) :
getNextItem($currentItem);
showItem($currentItem, $itemToShow);
};
$prevBtn.bind('click', { prev : true }, handleBtnClick);
$nextBtn.bind('click', { next : true }, handleBtnClick);
$items.eq(0).fadeIn(settings.fadeDuration, fadeCallback);
});
};
$.fn.slideshow.defaults = {
fadeDuration : 1000,
interval : 5000,
prevButtonText : 'previous',
nextButtonText : 'next'
};
})(jQuery);
$('#slideshow').slideshow();