forked from rmurphey/jqfundamentals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslideshow-objectLiteral.js
122 lines (95 loc) · 2.36 KB
/
slideshow-objectLiteral.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
bank.slideshow = {
timeout : null,
manualMode : false,
settings : {
fadeDuration : 500,
interval : 1000,
prevButtonText : 'Previous',
nextButtonText : 'Next'
},
init : function(opts) {
this.$slideshow = $('#slideshow');
this.$counter = $('<div/>').insertAfter(this.$slideshow);
this.$items = $slideshow.find('li').hide();
this.totalItems = $items.length;
this.$items.eq(0).fadeIn(this.settings.fadeDuration, fadeCallback);
},
getNextItem : function($item) {
return $item.next().length ?
$item.next() : this.$items.first();
},
getPrevItem : function($item) {
return $item.prev().length ?
$item.prev() : this.$items.last();
},
showItem : function($curr, $toShow) {
var $toShow = $toShow || getNextItem($curr),
self = this;
$curr.fadeOut(this.fadeDuration, function() {
$toShow.fadeIn(this.fadeDuration, function() {
var showingEl = this;
$.proxy(function() {
this.showCallback(showingEl);
}, self);
});
});
},
showCallback : function(showingEl) {
if (this.manualMode) { return; }
var $showingEl = $(el),
$next = getNextItem($showingEl),
num = $showingEl.prevAll().length + 1;
this.updateCounter(num);
this.timeout = setTimeout(
// must be $.proxy'd, otherwise
// "this" will be the window object
$.proxy(function() {
this.showItem($showingEl, $next);
}, this),
this.interval);
},
setupButtons : function() {
var $controls = $('<div/>').insertAfter(this.$slideshow);
$('<input/>', {
type : 'button',
value : this.prevButtonText
})
.appendTo($controls)
.bind(
'click',
{ prev : true },
$.proxy(this.handleButton, this)
);
$('<input/>', {
type : 'button',
value : this.nextButtonText
})
.appendTo($controls)
.bind(
'click',
{ prev : false },
$.proxy(this.handleButton, this)
);
},
updateCounter : function(curr) {
this.$counter.text(curr + ' of ' + this.totalItems);
},
handleButton : function(e) {
if (!this.manualMode) {
clearTimeout(this.timeout);
this.manualMode = true;
}
this.showItem(
this.$items.filter(':visible'),
$itemToShow = e.data.prev ?
this.getPrevItem($currentItem) :
this.getNextItem($currentItem)
);
}
};
$(document).ready(function() {
slideshow.init();
setTimeout(function() {
slideshow.settings.fadeDuration = 300;
}, 5000);
});