|
99 | 99 | } |
100 | 100 |
|
101 | 101 | var self = this; |
102 | | - $('<div id="lightboxOverlay" class="lightboxOverlay"></div><div id="lightbox" class="lightbox"><div class="lb-outerContainer"><div class="lb-container"><img class="lb-image" src="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" alt=""/><div class="lb-nav"><a class="lb-prev" aria-label="Previous image" href="" ></a><a class="lb-next" aria-label="Next image" href="" ></a></div><div class="lb-loader"><a class="lb-cancel"></a></div></div></div><div class="lb-dataContainer"><div class="lb-data"><div class="lb-details"><span class="lb-caption"></span><span class="lb-number"></span></div><div class="lb-closeContainer"><a class="lb-close"></a></div></div></div></div>').appendTo($('body')); |
| 102 | + |
| 103 | + // The two root notes generated, #lightboxOverlay and #lightbox are given |
| 104 | + // tabindex attrs so they are focusable. We attach our keyboard event |
| 105 | + // listeners to these two elements, and not the document. Clicking anywhere |
| 106 | + // while Lightbox is opened will keep the focus on or inside one of these |
| 107 | + // two elements. |
| 108 | + // |
| 109 | + // We do this so we can prevent propogation of the Esc keypress when |
| 110 | + // Lightbox is open. This prevents it from intefering with other components |
| 111 | + // on the page below. |
| 112 | + // |
| 113 | + // Github issue: https://github.com/lokesh/lightbox2/issues/663 |
| 114 | + $('<div id="lightboxOverlay" tabindex="-1" class="lightboxOverlay"></div><div id="lightbox" tabindex="-1" class="lightbox"><div class="lb-outerContainer"><div class="lb-container"><img class="lb-image" src="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" alt=""/><div class="lb-nav"><a class="lb-prev" aria-label="Previous image" href="" ></a><a class="lb-next" aria-label="Next image" href="" ></a></div><div class="lb-loader"><a class="lb-cancel"></a></div></div></div><div class="lb-dataContainer"><div class="lb-data"><div class="lb-details"><span class="lb-caption"></span><span class="lb-number"></span></div><div class="lb-closeContainer"><a class="lb-close"></a></div></div></div></div>').appendTo($('body')); |
103 | 115 |
|
104 | 116 | // Cache jQuery objects |
105 | 117 | this.$lightbox = $('#lightbox'); |
|
387 | 399 | self.$lightbox.find('.lb-dataContainer').width(newWidth); |
388 | 400 | self.$lightbox.find('.lb-prevLink').height(newHeight); |
389 | 401 | self.$lightbox.find('.lb-nextLink').height(newHeight); |
| 402 | + |
| 403 | + // Set focus on one of the two root nodes so keyboard events are captured. |
| 404 | + self.$overlay.focus(); |
| 405 | + |
390 | 406 | self.showImage(); |
391 | 407 | } |
392 | 408 |
|
|
493 | 509 | }; |
494 | 510 |
|
495 | 511 | Lightbox.prototype.enableKeyboardNav = function() { |
496 | | - $(document).on('keyup.keyboard', $.proxy(this.keyboardAction, this)); |
| 512 | + this.$lightbox.on('keyup.keyboard', $.proxy(this.keyboardAction, this)); |
| 513 | + this.$overlay.on('keyup.keyboard', $.proxy(this.keyboardAction, this)); |
497 | 514 | }; |
498 | 515 |
|
499 | 516 | Lightbox.prototype.disableKeyboardNav = function() { |
500 | | - $(document).off('.keyboard'); |
| 517 | + this.$lightbox.off('.keyboard'); |
| 518 | + this.$overlay.off('.keyboard'); |
501 | 519 | }; |
502 | 520 |
|
503 | 521 | Lightbox.prototype.keyboardAction = function(event) { |
|
507 | 525 |
|
508 | 526 | var keycode = event.keyCode; |
509 | 527 | if (keycode === KEYCODE_ESC) { |
| 528 | + // Prevent bubbling so as to not affect other components on the page. |
| 529 | + event.stopPropagation(); |
510 | 530 | this.end(); |
511 | 531 | } else if (keycode === KEYCODE_LEFTARROW) { |
512 | 532 | if (this.currentImageIndex !== 0) { |
|
0 commit comments