-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimple-gallery.js
215 lines (186 loc) · 7.09 KB
/
simple-gallery.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* simple-gallery.js - v2.6.0
* Author: Alex Kalicki (https://github.com/akalicki)
*
* simple-gallery.js is a lightweight jQuery extension for quickly creating
* highly customizable photo galleries using standard jQuery notation.
*
* All work contained in this file is released under the MIT License.
* Please contact the copyright holder with further questions.
*/
;(function ($, window, document, undefined) {
$.widget('ak.gallery', {
// default widget options
options: {
source: "",
thumbExt: "",
animate: true,
startImg: 0,
waitTime: 5000,
changeTime: 700,
easing: "swing",
restartOnEnd: true,
selectClass: "selected",
showCaptions: false,
captionTarget: ""
},
// setup gallery widget
_create: function() {
this.images = $(this.options.source);
this.nextImg = this.options.startImg;
this.element.css({
"background-repeat": "no-repeat",
"background-position": "center",
"opacity": 0
});
this.images.on('click.gallery', $.proxy(this._onClick, this));
this._loadNext();
},
// reset DOM to state before gallery was invoked
_destroy: function() {
this._trigger("destroy");
this.images.off('click.gallery');
this.images.removeClass(this.options.selectClass);
window.clearTimeout(this.cycle);
this.element.stop(true);
if (this.options.showCaptions) {
$(this.options.captionTarget).stop(true);
}
this._startTransition();
var self = this;
this.element.queue(function(next) {
self.element.css({
"background-repeat": "repeat",
"background-position": "0% 0%",
"background-image": "none",
"opacity": 1
});
next();
});
this._super();
},
// allow setting of options during/after construction
_setOption: function(key, value) {
this._trigger('optionchange');
this.options[key] = value;
if (key === "source") {
this._trigger('sourcechange');
window.clearTimeout(this.cycle);
this.images.off('click.gallery');
this.images = $(this.options.source);
this.images.on('click.gallery', $.proxy(this._onClick, this));
this.nextImg = this.options.startImg;
this._changeToImg(this.nextImg);
}
},
// load the next image in cycle
_loadNext: function() {
if (this.nextImg < this.images.length) { // more images in cycle
this._changeToImg(this.nextImg);
}
else if (this.options.restartOnEnd) { // end of cycle, restart
this.nextImg = 0;
this._changeToImg(this.nextImg);
}
},
// transition out, select new image, transition in
_changeToImg: function(index) {
var self = this;
self.element.clearQueue();
self._startTransition();
self.element.queue(function(next) {
self._selectImg(index);
next();
});
self._endTransition();
if (self.options.animate) {
self.element.queue(function(next) {
self.cycle = window.setTimeout(function() {
self._loadNext();
}, self.options.waitTime);
next();
});
}
},
// select a new image to be placed in target
_selectImg: function(index) {
this._trigger('imageload');
var selected = this.images.get(index);
var url = this._removeLast(selected.src, this.options.thumbExt);
this.element.css("background-image", "url(" + url + ")");
this.images.removeClass(this.options.selectClass);
selected.className += this.options.selectClass;
this.nextImg = index + 1;
if (this.options.showCaptions) {
var caption = selected.title;
$(this.options.captionTarget).html(caption);
}
},
// perform transition to remove current image from target
_startTransition: function() {
this.element.animate(
{"opacity": 0},
this.options.changeTime,
this.options.easing
);
if (this.options.showCaptions) {
$(this.options.captionTarget).animate(
{"opacity": 0},
this.options.changeTime,
this.options.easing
);
}
},
// perform transition to add new image to target
_endTransition: function() {
this.element.animate(
{"opacity": 1},
this.options.changeTime,
this.options.easing
);
if (this.options.showCaptions) {
$(this.options.captionTarget).animate(
{"opacity": 1},
this.options.changeTime,
this.options.easing
);
}
},
// reset timer and select image if clicked
_onClick: function(event) {
this._trigger('click');
window.clearTimeout(this.cycle);
this.element.stop(true);
this.nextImg = this.images.index($(event.target));
this._changeToImg(this.nextImg);
},
// stop photos from switching
stopAnimation: function() {
this._trigger('animationstop');
this.options.animate = false;
window.clearTimeout(this.cycle);
this.element.stop(true);
if (this.options.showCaptions) {
$(this.options.captionTarget).stop(true);
}
this._endTransition();
},
// resume photo switching
resumeAnimation: function() {
var self = this;
self._trigger('animationresume');
self.options.animate = true;
self.cycle = window.setTimeout(function() {
self._loadNext();
}, self.options.waitTime);
},
// helper: remove last occurence of substring
_removeLast: function(str, substr) {
var lastPos = str.lastIndexOf(substr);
if (lastPos == -1)
return str;
return str.substring(0, lastPos) +
str.substring(lastPos + substr.length);
}
});
})(jQuery, window, document);