forked from EnzoMartin/Simple-Rotating-Banner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.simplebanner.js
More file actions
202 lines (185 loc) · 5.45 KB
/
jquery.simplebanner.js
File metadata and controls
202 lines (185 loc) · 5.45 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* Written by Enzo Martin
*
* GitHub: https://github.com/EnzoMartin78/
* Twitter: EnzoMartin
*/
;(function($, window, undefined) {
"use strict";
/**
* Simple Banner constructor
* @param element
* @param options
* @constructor
*/
var Simplebanner = function(element, options) {
this.element = element;
this._paused = false;
this._timer = {};
this._currentBanner = {};
this._newBanner = {};
this._bannerWidth = 0;
this._bannerCount = 0;
this.options = $.extend({
arrows: true,
indicators: true,
pauseOnHover: true,
autoRotate: true,
rotateTimeout: 5000,
animTime: 300
}, options);
this.init();
};
/**
* Initializer
*/
Simplebanner.prototype.init = function() {
this._bannerCount = this.element.find('.bannerList li').length;
this._bannerWidth = this.element.find('.bannerList li').outerWidth();
if(!this._bannerWidth){
this._bannerWidth = this.element.width();
}
this._currentBanner = this.element.find('.bannerList li:first').addClass('current');
if(this.options.indicators){
this.buildIndicators();
} else {
this.element.addClass('hiddenIndicators');
}
if(!this.options.arrows){
this.element.addClass('hiddenArrows');
}
if(this._bannerCount > 1 && this.options.autoRotate){
this.toggleTimer();
}
this.bindEvents();
};
// This sets the basic events based off the options selected
Simplebanner.prototype.bindEvents = function() {
var self = this;
if(self.options.indicators){
self.element.find('.bannerIndicator').on({
'click': function() {
if (!$(this).hasClass('active')) {
var slideIndex = $(this).index();
self._newBanner = self.element.find('.bannerList li:eq(' + slideIndex + ')');
self.goToBanner(slideIndex);
}
}
});
}
if(self.options.arrows){
self.element.find('.bannerControlsWpr').on({
'click': function() {
if($(this).hasClass('bannerControlsPrev')){
self.previousBanner();
} else {
self.nextBanner();
}
}
});
}
if(self.options.pauseOnHover && self.options.autoRotate){
self.element.on({
"mouseenter": function() {
self.toggleTimer(true);
},
"mouseleave": function() {
self.toggleTimer(false);
}
});
}
};
// Goes to the next banner - loops back to the first banner
Simplebanner.prototype.nextBanner = function() {
if (this._currentBanner.next().length) {
this._newBanner = this._currentBanner.next();
} else {
this._newBanner = this.element.find('.bannerList li:first');
}
this.goToBanner(this._newBanner.index());
};
// Goes to the previous banner - loops back to the last banner
Simplebanner.prototype.previousBanner = function() {
if (this._currentBanner.prev().length) {
this._newBanner = this._currentBanner.prev();
} else {
this._newBanner = this.element.find('.bannerList li:last');
}
this.goToBanner(this._newBanner.index());
};
/**
* Goes to a specific slide - This is called by both the Previous and Next methods as well as the Indicator buttons
* @param slideIndex
*/
Simplebanner.prototype.goToBanner = function(slideIndex) {
var self = this;
self._currentBanner.removeClass('current');
self.element.find('.bannerIndicators .current').removeClass('current');
self._currentBanner = self._newBanner;
self._currentBanner.addClass('current');
self.element.find('.bannerIndicators li:eq(' + slideIndex + ')').addClass('current');
self.element.find('.bannerList').stop(false, true).animate({
'marginLeft': -slideIndex * self._bannerWidth
},self.options.animTime);
};
// Create the correct amount of indicators based off total banners
Simplebanner.prototype.buildIndicators = function() {
var self = this;
var indicatorUl = self.element.find('.bannerIndicators ul');
self.element.find('.bannerList li').each(function(){
indicatorUl.append('<li class="bannerIndicator"></li>');
});
indicatorUl.find('li:first').addClass('current');
};
/**
* Starts or stops the timer for going to the next banner
* @param timer
*/
Simplebanner.prototype.toggleTimer = function(timer) {
var self = this;
clearTimeout(self._timer);
if(!timer){
self._timer = setTimeout(function(){
self.nextBanner();
self.toggleTimer(false);
},self.options.rotateTimeout);
}
};
// jQuery wrapper method
$.fn.simplebanner = function(options) {
var method, args, ret = false;
if (typeof options === "string") {
args = [].slice.call(arguments, 0);
}
this.each(function() {
var self = $(this);
var instance = self.data("stickyInstance");
if(!self.attr('id')){
self.attr('id','simpleBanner-' + ($.fn.simplebanner._instances.length+1));
}
if (instance && options) {
if (typeof options === "object") {
ret = $.extend(instance.options, options);
} else if (options === "options") {
ret = instance.options;
} else if (typeof instance[options] === "function") {
ret = instance[options].apply(instance, args.slice(1));
} else {
throw new Error('Simple Banner has no option/method named "' + method + '"');
}
} else {
instance = new Simplebanner(self, options || {});
self.data("stickyInstance", instance);
$.fn.simplebanner._instances.push(instance);
}
});
return ret || this;
};
$.fn.simplebanner._instances = [];
// Deathstar death beam
$(document).on("pageleave", function () {
$.each($.fn.simplebanner._instances, function() {
this.children().off();
});
$.fn.simplebanner._instances = [];
});
}($, window));