|
15 | 15 | slider = $('<div id="gallerySlider">'), |
16 | 16 | prevArrow = $('<a id="prevArrow"></a>'), |
17 | 17 | 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; |
19 | 22 |
|
20 | 23 |
|
21 | 24 | /* Creating the plugin */ |
|
45 | 48 | } |
46 | 49 | placeholders = placeholders.add($('<div class="placeholder">')); |
47 | 50 | }); |
| 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 | + } |
48 | 57 |
|
49 | 58 | // Hide the gallery if the background is touched / clicked |
50 | 59 | slider.append(placeholders).on('click',function(e){ |
|
188 | 197 | showNext(); |
189 | 198 | } |
190 | 199 |
|
| 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 | + |
191 | 214 | }); |
192 | 215 | //listen for orientationchange |
193 | 216 | $(window).bind('orientationchange',function(){ |
194 | | - |
195 | | - if($('.image-caption').length > 0) |
| 217 | + if(captionContainer.is(":visible")) |
196 | 218 | { |
197 | | - //change the width of the caption div |
198 | | - $('.image-caption').css('width',$('.image-caption').prev('img').width()); |
| 219 | + showCaption(index); |
199 | 220 | } |
| 221 | + |
| 222 | + |
200 | 223 | }); |
201 | 224 |
|
202 | 225 | /* Private functions */ |
|
229 | 252 | if(!overlayVisible){ |
230 | 253 | return false; |
231 | 254 | } |
| 255 | + if(captionContainer.is(":visible")) |
| 256 | + { |
| 257 | + |
| 258 | + captionContent.text(''); |
| 259 | + captionContainer.hide(); |
| 260 | + } |
232 | 261 |
|
233 | 262 | // Hide the overlay |
234 | 263 | overlay.hide().removeClass('visible'); |
|
237 | 266 | //Clear preloaded items |
238 | 267 | $('.placeholder').empty(); |
239 | 268 |
|
240 | | - |
| 269 | + |
241 | 270 | //Reset possibly filtered items |
242 | 271 | items = allitems; |
243 | 272 | } |
|
273 | 302 |
|
274 | 303 | if(hasCaption) |
275 | 304 | { |
| 305 | + |
276 | 306 | setTimeout(function(){ |
277 | 307 | showCaption(index); |
278 | 308 | },500); |
|
304 | 334 | // If this is not the last image |
305 | 335 | if(index+1 < items.length){ |
306 | 336 | 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 |
310 | 345 | if(captions[index]!=undefined) |
311 | 346 | { |
| 347 | + |
312 | 348 | setTimeout(function(){ |
313 | 349 | showCaption(index); |
| 350 | + |
314 | 351 | },500); |
315 | 352 | } |
316 | | - offsetSlider(index); |
317 | 353 |
|
318 | 354 | preload(index+1); |
| 355 | + |
319 | 356 | } |
320 | 357 |
|
321 | 358 | else{ |
322 | 359 | // Trigger the spring animation |
323 | 360 | slider.addClass('rightSpring'); |
| 361 | + captionContainer.addClass('rightSpring'); |
| 362 | + |
324 | 363 | setTimeout(function(){ |
325 | 364 | slider.removeClass('rightSpring'); |
| 365 | + captionContainer.removeClass('rightSpring'); |
| 366 | + |
326 | 367 | },500); |
327 | 368 | } |
328 | 369 | } |
|
333 | 374 | if(index>0){ |
334 | 375 | index--; |
335 | 376 |
|
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 |
340 | 384 | if(captions[index]!=undefined) |
341 | 385 | { |
| 386 | + |
342 | 387 | setTimeout(function(){ |
343 | 388 | showCaption(index); |
| 389 | + |
344 | 390 | },500); |
345 | 391 | } |
346 | 392 |
|
347 | | - offsetSlider(index); |
348 | | - |
349 | 393 | preload(index-1); |
| 394 | + |
350 | 395 | } |
351 | 396 |
|
352 | 397 | else{ |
353 | 398 | // Trigger the spring animation |
354 | 399 | slider.addClass('leftSpring'); |
| 400 | + captionContainer.addClass('leftSpring'); |
| 401 | + |
355 | 402 | setTimeout(function(){ |
356 | 403 | slider.removeClass('leftSpring'); |
| 404 | + captionContainer.removeClass('leftSpring'); |
| 405 | + |
357 | 406 | },500); |
358 | 407 | } |
359 | 408 | } |
360 | 409 |
|
361 | 410 | 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 | + |
380 | 422 | } |
| 423 | + |
| 424 | + |
381 | 425 | }; |
382 | 426 |
|
383 | 427 | })(jQuery); |
0 commit comments