This repository has been archived by the owner on Mar 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.yalx.js
263 lines (218 loc) · 7.38 KB
/
jquery.yalx.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*!
* YALx - Yet Another Lightbox
* Copyright (c) 2009 Johan Sahlén
* Licensed under the MIT license.
*/
(function($){
/**
* Main plugin function.
* @param [opts] The options you give here will extend
* {@link $.fn.YALx.defaults}
*/
$.fn.YALx = function(opts) {
var opts = $.extend({}, $.fn.YALx.defaults, opts);
var links = $(this), elements = {};
var visible = false, working = false, currentIndex = 0;
var win = $(window), doc = $(document);
var currentWidth = 0, currentHeight = 0;
/**
* Load a new image.
* @param {String} src The URI of the new image
*/
var loadImage = function(src, index) {
currentIndex = index;
_show();
var currentImage = elements.container.find('img');
if (currentImage.attr('src') == src) return;
showLoader();
var preloader = new Image;
preloader.src = src;
$(preloader).bind('load', function() {
hideLoader();
if (currentImage.length) {
currentImage.animate({ opacity: 0 }, opts.imageFadeSpeed, null, function() {
elements.container.find('img').remove();
_processImage(src, preloader.width, preloader.height);
})
} else {
_processImage(src, preloader.width, preloader.height);
}
}).bind('error', function() {
hideLoader();
});
};
/**
* After an image has been downloaded.
*/
var _processImage = function(src, width, height) {
var container = elements.container;
var padding = opts.padding * 2;
var image = $('<img src="'+src+'"></img>').css({ opacity: 0 });
if (!(width != currentWidth || height != currentHeight)) {
container.append(image);
image.animate({ opacity: 1 }, opts.imageFadeSpeed);
return;
}
var animationOpts = {
width: width,
height: height,
left: ((doc.width() - (width + padding)) / 2),
top: (((win.height() - (height + padding)) / 2) + win.scrollTop())
};
container.animate(animationOpts, opts.resizeSpeed, opts.easing, function() {
container.append(image);
image.animate({ opacity: 1 }, opts.imageFadeSpeed);
});
currentWidth = width;
currentHeight = height;
};
/**
* Show the lightbox. Bind window events.
*/
var _show = function() {
if (visible) return;
_createElements();
var backdrop = elements.backdrop;
var container = elements.container;
backdrop
.css({ width: doc.width(), height: doc.height(), display: 'block' })
.animate({ opacity: opts.backdropOpacity }, opts.fadeInSpeed);
if (!container.find('img').length) {
var dimensions = opts.loaderDimensions;
container.css({ width: dimensions[0], height: dimensions[1] });
}
elements.container
.css({
display: 'block',
left: ((doc.width() - container.width()) / 2),
top: (((win.height() - container.height()) / 2) + win.scrollTop())
})
.animate({ opacity: 1 }, opts.fadeInSpeed);
if (elements.help) elements.help
.css({ display: 'block' })
.animate({ opacity: 1 }, opts.fadeInSpeed);
visible = true;
win.bind('keyup.yalx', _handleKeypress);
win.bind('scroll.yalx', _repositionContainer);
};
/**
* Create the DOM elements for the lightbox.
*/
var _createElements = function() {
// Do nothing if they already exist
if (elements.backdrop) return;
// Create the elements and make them invisible
var backdrop = $('<div id="'+opts.backdropId+'"></div>').css({ display: 'none', opacity: 0 });
var container = $('<div id="'+opts.containerId+'"></div>').css({ display: 'none', opacity: 0 });
var loader = $('<div class="'+opts.loaderClass+'"></div>').hide();
var help = null;
if (opts.help) help = $('<p id="'+opts.helpId+'">'+opts.help+'</p>').css({ display: 'none', opacity: 0 });
backdrop.click(hide);
container.append(loader);
$('body')
.append(backdrop)
.append(container)
.append(help);
elements = {
backdrop: backdrop,
container: container,
loader: loader,
help: help
};
};
/**
* Reposition the container based on scroll position.
*/
var _repositionContainer = function() {
var container = elements.container;
if (!container.is(':animated')) {
container.css({ top: (((win.height() - container.height()) / 2) + win.scrollTop()) });
}
};
/**
* Step to the next or previous picture.
* @param {String} [direction='next'] 'next' or 'previous'
*/
var step = function(direction) {
var direction = direction || 'next';
var newIndex = -1;
if (direction == 'previous' && currentIndex > 0) newIndex = currentIndex - 1;
else if (direction == 'next' && currentIndex < links.length-1) newIndex = currentIndex + 1;
if (newIndex >= 0) {
var link = links.slice(newIndex, newIndex+1);
loadImage(link.attr('href'), newIndex);
}
};
/**
* Hide the lightbox. Unbind window events.
*/
var hide = function() {
elements.backdrop.add(elements.container).add(elements.help).animate({ opacity: 0 }, opts.fadeOutSpeed, null, function() {
$(this).css({ display: 'none' });
visible = false;
win.unbind('keyup.yalx');
win.unbind('scroll.yalx');
});
};
/**
* Show the loader. Sets 'working' status to true and positions the
* loader within the container before displaying it.
*/
var showLoader = function() {
var loader = elements.loader;
var container = elements.container;
var dimensions = opts.loaderDimensions;
working = true;
loader.css({
top: (container.innerHeight() - dimensions[1]) / 2,
left: (container.innerWidth() - dimensions[0]) / 2
});
loader.show();
};
/**
* Hide the loader. Also set 'working' status to false.
*/
var hideLoader = function() {
working = false;
elements.loader.hide();
};
/**
* Handle all keypress events while the lightbox is visible. ESC hides
* the lightbox, left arrow steps to the previous picture, right arrow
* steps to the next one.
*/
var _handleKeypress = function(e) {
if (working) return;
if (e.keyCode == 37) step('previous');
else if (e.keyCode == 39) step('next');
else if (e.keyCode == 27) hide();
};
// Bind events
links.each(function(index) {
var link = $(this);
link.click(function() {
loadImage(link.attr('href'), index);
return false;
});
});
};
/**
* Default options. Overwrite or extend this to set options for all uses
* of the plugin.
*/
$.fn.YALx.defaults = {
loaderDimensions: [32,32],
backdropId: "yalx_backdrop",
containerId: "yalx",
loaderClass: "loader",
padding: 5,
backdropOpacity: 0.9,
easing: "swing",
resizeSpeed: 200,
fadeInSpeed: 200,
fadeOutSpeed: 100,
imageFadeSpeed: 100,
helpId: "yalx_help",
help: "You can use your left/right arrow keys to navigate between pictures. ESC to close."
};
}(jQuery));