forked from terrymun/Fluidbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.fluidbox.js
374 lines (321 loc) · 11.5 KB
/
jquery.fluidbox.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Fluidbox
// Description: Replicating the seamless lightbox transition effect seen on Medium.com, with some improvements
// Version: 1.3.4
// Author: Terry Mun
// Author URI: http://terrymun.com
// --------------------------------------------------------
// Dependency: Paul Irish's jQuery debounced resize event
// --------------------------------------------------------
(function($,sr){
// debouncing function from John Hann
// http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
var debounce = function (func, threshold, execAsap) {
var timeout;
return function debounced () {
var obj = this, args = arguments;
function delayed () {
if (!execAsap)
func.apply(obj, args);
timeout = null;
};
if (timeout)
clearTimeout(timeout);
else if (execAsap)
func.apply(obj, args);
timeout = setTimeout(delayed, threshold || 100);
};
}
// smartresize
jQuery.fn[sr] = function(fn){ return fn ? this.bind('resize', debounce(fn)) : this.trigger(sr); };
})(jQuery,'smartresize');
// -----------------------------
// Fluidbox plugin starts here
// -----------------------------
(function ($) {
$.fn.fluidbox = function (opts) {
// Default settings
var settings = $.extend(true, {
viewportFill: 0.95,
overlayColor: 'rgba(255,255,255,.85)',
debounceResize: true,
stackIndex: 1000,
stackIndexDelta: 10,
closeTrigger: [
{
selector: '.fluidbox-overlay',
event: 'click'
},
{
selector: 'document',
event: 'keyup',
keyCode: 27
}
]
}, opts);
// Ensure that the stackIndex does not become negative
if(settings.stackIndex < settings.stackIndexDelta) settings.stackIndexDelta = settings.stackIndex;
// Dynamically create overlay
$fbOverlay = $('<div />', {
class: 'fluidbox-overlay',
css: {
'background-color': settings.overlayColor,
'z-index': settings.stackIndex
}
});
// Declare variables
var $fb = this,
$w = $(window), // Shorthand for $(window)
vpRatio,
// Function:
// 1. funcCloseFb() - used to close any instance of opened Fluidbox
// 2. funcPositionFb() - used for dynamic positioning of any instance of opened Fluidbox
// 3. funcCalcAll() - used to run funcCalc() for every instance of targered Fluidbox thumbnail
// 4. funcCalc() - used to store dimensions of image, ghost element and wrapper element upon initialization or resize
// 5. fbClickhandler() - universal click handler for all Fluidbox items
funcCloseFb = function () {
$('.fluidbox-opened').trigger('click');
},
funcPositionFb = function ($activeFb) {
// Get shorthand for more objects
var $img = $activeFb.find('img'),
$ghost = $activeFb.find('.fluidbox-ghost'),
$wrap = $activeFb.find('.fluidbox-wrap'),
$data = $activeFb.data(),
fHeight = 0,
fWidth = 0;
// Check natural dimensions
if(vpRatio > $img.data().imgRatio) {
if($data.natHeight < $w.height()*settings.viewportFill) {
fHeight = $data.natHeight;
} else {
fHeight = $w.height()*settings.viewportFill;
}
$data.imgScale = fHeight/$img.height();
} else {
if($data.natWidth < $w.width()*settings.viewportFill) {
fWidth = $data.natWidth;
} else {
fWidth = $w.width()*settings.viewportFill;
}
$data.imgScale = fWidth/$img.width();
}
// Calculation goes here
var offsetY = $w.scrollTop()-$img.offset().top+0.5*($img.data('imgHeight')*($img.data('imgScale')-1))+0.5*($w.height()-$img.data('imgHeight')*$img.data('imgScale')),
offsetX = 0.5*($img.data('imgWidth')*($img.data('imgScale')-1))+0.5*($w.width()-$img.data('imgWidth')*$img.data('imgScale'))-$img.offset().left,
scale = $data.imgScale;
// Apply CSS transforms to ghost element
// For offsetX and Y, we round to one decimal place
// For scale, we round to three decimal places
$ghost.css({
'transform': 'translate('+parseInt(offsetX*10)/10+'px,'+parseInt(offsetY*10)/10+'px) scale('+parseInt(scale*1000)/1000+')',
top: $img.offset().top - $wrap.offset().top,
left: $img.offset().left - $wrap.offset().left
});
},
funcCalcAll = function() {
$fb.each(function () {
funcCalc($(this));
});
},
funcCalc = function ($fbItem) {
// Get viewport ratio
vpRatio = $w.width() / $w.height();
// Get image dimensions and aspect ratio
if($fbItem.hasClass('fluidbox')) {
var $img = $fbItem.find('img'),
$ghost = $fbItem.find('.fluidbox-ghost'),
$wrap = $fbItem.find('.fluidbox-wrap'),
data = $img.data();
function imageProp() {
// Store image dimensions in jQuery object
data.imgWidth = $img.width();
data.imgHeight = $img.height();
data.imgRatio = $img.width()/$img.height();
// Resize and position ghost element
$ghost.css({
width: $img.width(),
height: $img.height(),
top: $img.offset().top - $wrap.offset().top + parseInt($img.css('borderTopWidth')) + parseInt($img.css('paddingTop')),
left: $img.offset().left - $wrap.offset().left + parseInt($img.css('borderLeftWidth')) + parseInt($img.css('paddingLeft'))
});
// Calculate scale based on orientation
if(vpRatio > data.imgRatio) {
data.imgScale = $w.height()*settings.viewportFill/$img.height();
} else {
data.imgScale = $w.width()*settings.viewportFill/$img.width();
}
}
imageProp();
// Rerun everything on imageload, to overcome issue in Firefox
$img.load(imageProp);
}
},
fbClickHandler = function(e) {
// Check if the fluidbox element does have .fluidbox assigned to it
if($(this).hasClass('fluidbox')) {
// Variables
var $activeFb = $(this),
$img = $(this).find('img'),
$ghost = $(this).find('.fluidbox-ghost'),
$wrap = $(this).find('.fluidbox-wrap'),
timer = {};
if($(this).data('fluidbox-state') === 0 || !$(this).data('fluidbox-state')) {
// State: Closed
// Action: Open fluidbox
// Wait for ghost image to be loaded successfully first, then do the rest
$('<img />', {
src: $img.attr('src')
}).load(function () {
// Preload ghost image
$('<img />', {
src: $activeFb.attr('href')
}).load(function() {
// Store natural width and heights
$activeFb
.data('natWidth', $(this)[0].naturalWidth)
.data('natHeight', $(this)[0].naturalHeight);
// What are we doing here:
// 1. Append overlay in fluidbox
// 2. Toggle fluidbox state with data attribute
// 3. Store original z-index with data attribute (so users can change z-index when they see fit in CSS file)
// 4. Class toggle
$activeFb
.append($fbOverlay)
.data('fluidbox-state', 1)
.removeClass('fluidbox-closed')
.addClass('fluidbox-opened');
// Force timer to completion
if(timer['close']) window.clearTimeout(timer['close']);
// Set timer for opening
timer['open'] = window.setTimeout(function() {
// Show overlay
$('.fluidbox-overlay').css({ opacity: 1 });
}, 10);
// Change wrapper z-index, so it is above everything else
// Decrease all siblings z-index by 1 just in case
$('.fluidbox-wrap').css({ zIndex: settings.stackIndex - settings.stackIndexDelta - 1 });
$wrap.css({ 'z-index': settings.stackIndex + settings.stackIndexDelta });
// Set thumbnail image source as background image first, preload later
$ghost.css({
'background-image': 'url('+$img.attr('src')+')',
opacity: 1
});
// Hide original image
$img.css({ opacity: 0 });
$ghost.css({ 'background-image': 'url('+$activeFb.attr('href')+')' });
// Position Fluidbox
funcPositionFb($activeFb);
});
});
} else {
// State: Open
// Action: Close fluidbox
// Switch state
$activeFb
.data('fluidbox-state', 0)
.removeClass('fluidbox-opened')
.addClass('fluidbox-closed');
// Set timer for closing
if(timer['open']) window.clearTimeout(timer['open']);
timer['close'] = window.setTimeout(function() {
$('.fluidbox-overlay').remove();
$wrap.css({ 'z-index': settings.stackIndex - settings.stackIndexDelta });
}, 10);
// Hide overlay
$('.fluidbox-overlay').css({ opacity: 0 });
// Reverse animation on wrapped elements, and restore stacking order
// You might want to change this value if your transition timing is longer
$ghost.css({
'transform': 'translate(0,0) scale(1)',
opacity: 0,
top: $img.offset().top - $wrap.offset().top + parseInt($img.css('borderTopWidth')) + parseInt($img.css('paddingTop')),
left: $img.offset().left - $wrap.offset().left + parseInt($img.css('borderLeftWidth')) + parseInt($img.css('paddingLeft'))
});
$img.css({ opacity: 1 });
}
e.preventDefault();
}
};
// When should we close Fluidbox?
if(settings.closeTrigger) {
// Go through array
$.each(settings.closeTrigger, function (i) {
var trigger = settings.closeTrigger[i];
// Attach events
if(trigger.selector != 'window') {
// If it is not 'window', we append click handler to $(document) object, allow it to bubble up
// However, if thes selector is 'document', we use a different .on() syntax
if(trigger.selector == 'document') {
if(trigger.keyCode) {
$(document).on(trigger.event, function (e) {
if(e.keyCode == trigger.keyCode) funcCloseFb();
});
} else {
$(document).on(trigger.event, funcCloseFb);
}
} else {
$(document).on(trigger.event, settings.closeTrigger[i].selector, funcCloseFb);
}
} else {
// If it is 'window', append click handler to $(window) object
$w.on(trigger.event, funcCloseFb);
}
});
}
// Go through each individual object
$fb.each(function (i) {
// Check if Fluidbox:
// 1. Is an anchor element ,<a>
// 2. Contains one and ONLY one child
// 3. The only child is an image element, <img>
// 4. If the element is hidden
if($(this).is('a') && $(this).children('img').length === 1 && $(this).css('display') !== 'none' && $(this).parents().css('display') !=='none') {
// Define wrap
var $fbInnerWrap = $('<div />', {
class: 'fluidbox-wrap',
css: {
'z-index': settings.stackIndex - settings.stackIndexDelta
}
});
// Add class
var $fbItem = $(this);
$fbItem
.addClass('fluidbox')
.wrapInner($fbInnerWrap)
.find('img')
.css({ opacity: 1 })
.after('<div class="fluidbox-ghost" />')
.each(function(){
var $img = $(this);
if ($img.width() > 0 && $img.height() > 0) {
// if image is already loaded (from cache)
funcCalc($fbItem);
$fbItem.click(fbClickHandler);
} else {
// wait for image to load
$img.load(function(){
funcCalc($fbItem);
$fbItem.click(fbClickHandler);
});
}
});
}
});
// Listen to window resize event
// Check if user wants to debounce the resize event (it is debounced by default)
var funcResize = function () {
// Recalculate dimensions
funcCalcAll();
// Reposition Fluidbox, but only if one is found to be open
var $activeFb = $('a.fluidbox.fluidbox-opened');
if($activeFb.length > 0) funcPositionFb($activeFb);
}
if(settings.debounceResize) {
$(window).smartresize(funcResize);
} else {
$(window).resize(funcResize);
}
// Return to allow chaining
return $fb;
};
})(jQuery);