Skip to content

Commit 5119101

Browse files
committed
added orientationchange and resize listeners; made the spring animation
also trigger on the caption container;
1 parent 2504c68 commit 5119101

1 file changed

Lines changed: 78 additions & 34 deletions

File tree

assets/touchTouch/touchTouch.jquery.js

Lines changed: 78 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
slider = $('<div id="gallerySlider">'),
1616
prevArrow = $('<a id="prevArrow"></a>'),
1717
nextArrow = $('<a id="nextArrow"></a>'),
18-
overlayVisible = false;
18+
captionContainer = $('<div id="caption-container"></div>'),
19+
captionContent = $('<div id="caption-content"></div>'),
20+
overlayVisible = false,
21+
resizeTimer = null;
1922

2023

2124
/* Creating the plugin */
@@ -45,6 +48,12 @@
4548
}
4649
placeholders = placeholders.add($('<div class="placeholder">'));
4750
});
51+
52+
if(captions.length > 0)
53+
{
54+
//overlay.append('<div id="caption-container"><p id="caption"></p></div>');
55+
captionContainer.append(captionContent).appendTo(overlay);
56+
}
4857

4958
// Hide the gallery if the background is touched / clicked
5059
slider.append(placeholders).on('click',function(e){
@@ -188,15 +197,29 @@
188197
showNext();
189198
}
190199

200+
});
201+
//listen for resize event of the browser
202+
$(window).bind('resize',function(){
203+
204+
205+
if(resizeTimer) clearTimeout(resizeTimer);
206+
resizeTimer = setTimeout(function(){
207+
if(captionContainer.is(":visible"))
208+
{
209+
210+
showCaption(index);
211+
}
212+
},100);
213+
191214
});
192215
//listen for orientationchange
193216
$(window).bind('orientationchange',function(){
194-
195-
if($('.image-caption').length > 0)
217+
if(captionContainer.is(":visible"))
196218
{
197-
//change the width of the caption div
198-
$('.image-caption').css('width',$('.image-caption').prev('img').width());
219+
showCaption(index);
199220
}
221+
222+
200223
});
201224

202225
/* Private functions */
@@ -229,6 +252,12 @@
229252
if(!overlayVisible){
230253
return false;
231254
}
255+
if(captionContainer.is(":visible"))
256+
{
257+
258+
captionContent.text('');
259+
captionContainer.hide();
260+
}
232261

233262
// Hide the overlay
234263
overlay.hide().removeClass('visible');
@@ -237,7 +266,7 @@
237266
//Clear preloaded items
238267
$('.placeholder').empty();
239268

240-
269+
241270
//Reset possibly filtered items
242271
items = allitems;
243272
}
@@ -273,6 +302,7 @@
273302

274303
if(hasCaption)
275304
{
305+
276306
setTimeout(function(){
277307
showCaption(index);
278308
},500);
@@ -304,25 +334,36 @@
304334
// If this is not the last image
305335
if(index+1 < items.length){
306336
index++;
307-
//remove existing caption
308-
$('.image-caption').remove();
309-
//if the image has the caption, add it
337+
338+
offsetSlider(index);
339+
//always hide the caption before showing it
340+
if(captions.length > 0)
341+
{
342+
captionContainer.hide();
343+
}
344+
//if the current image has a caption, show it
310345
if(captions[index]!=undefined)
311346
{
347+
312348
setTimeout(function(){
313349
showCaption(index);
350+
314351
},500);
315352
}
316-
offsetSlider(index);
317353

318354
preload(index+1);
355+
319356
}
320357

321358
else{
322359
// Trigger the spring animation
323360
slider.addClass('rightSpring');
361+
captionContainer.addClass('rightSpring');
362+
324363
setTimeout(function(){
325364
slider.removeClass('rightSpring');
365+
captionContainer.removeClass('rightSpring');
366+
326367
},500);
327368
}
328369
}
@@ -333,51 +374,54 @@
333374
if(index>0){
334375
index--;
335376

336-
//remove existing caption
337-
$('.image-caption').remove();
338-
339-
//if the image has the caption, add it
377+
offsetSlider(index);
378+
//always hide the caption before showing it
379+
if(captions.length > 0)
380+
{
381+
captionContainer.hide();
382+
}
383+
//if the current image has a caption, show it
340384
if(captions[index]!=undefined)
341385
{
386+
342387
setTimeout(function(){
343388
showCaption(index);
389+
344390
},500);
345391
}
346392

347-
offsetSlider(index);
348-
349393
preload(index-1);
394+
350395
}
351396

352397
else{
353398
// Trigger the spring animation
354399
slider.addClass('leftSpring');
400+
captionContainer.addClass('leftSpring');
401+
355402
setTimeout(function(){
356403
slider.removeClass('leftSpring');
404+
captionContainer.removeClass('leftSpring');
405+
357406
},500);
358407
}
359408
}
360409

361410
function showCaption(idx){
362-
363-
364-
var current_placeholder = placeholders.eq(idx);
365-
//get the width of the current image
366-
var img_width = current_placeholder.find('img').width();
367-
368-
var aCaption = $('<p class="image-caption"></p>');
369-
//set the width and text content for the caption div
370-
aCaption.text(captions[idx]).css('width',img_width)
371-
372-
current_placeholder.append(aCaption);
373-
374-
aCaption.fadeIn('slow');
375-
376-
377-
378-
379-
411+
412+
var current_placeholder = placeholders.eq(idx);
413+
var current_img = current_placeholder.find('img');
414+
//get the padding of the caption content
415+
var padding = parseInt(captionContent.css('padding-left'));
416+
//set the content and width of the caption
417+
captionContent.text(captions[idx]).css('width',current_img.width()- 2 * padding);
418+
//set the distance from the bottom for the caption container
419+
//fade in the caption container
420+
captionContainer.css({'bottom':current_placeholder.height() - current_img.height() - current_img.position().top}).fadeIn('slow');
421+
380422
}
423+
424+
381425
};
382426

383427
})(jQuery);

0 commit comments

Comments
 (0)