Skip to content

Commit 2c562d2

Browse files
committed
Merge pull request #14590 from twbs/carousel-keyboard-option
Add `keyboard` option to carousel
2 parents f75fc32 + 038a63b commit 2c562d2

File tree

3 files changed

+90
-2
lines changed

3 files changed

+90
-2
lines changed

docs/_includes/js/carousel.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@ <h3>Options</h3>
178178
<td>true</td>
179179
<td>Whether the carousel should cycle continuously or have hard stops.</td>
180180
</tr>
181+
<tr>
182+
<td>keyboard</td>
183+
<td>boolean</td>
184+
<td>true</td>
185+
<td>Whether the carousel should react to keyboard events.</td>
186+
</tr>
181187
</tbody>
182188
</table>
183189
</div><!-- /.table-responsive -->

js/carousel.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// =========================
1515

1616
var Carousel = function (element, options) {
17-
this.$element = $(element).on('keydown.bs.carousel', $.proxy(this.keydown, this))
17+
this.$element = $(element)
1818
this.$indicators = this.$element.find('.carousel-indicators')
1919
this.options = options
2020
this.paused =
@@ -23,6 +23,8 @@
2323
this.$active =
2424
this.$items = null
2525

26+
this.options.keyboard && this.$element.on('keydown.bs.carousel', $.proxy(this.keydown, this))
27+
2628
this.options.pause == 'hover' && !('ontouchstart' in document.documentElement) && this.$element
2729
.on('mouseenter.bs.carousel', $.proxy(this.pause, this))
2830
.on('mouseleave.bs.carousel', $.proxy(this.cycle, this))
@@ -35,7 +37,8 @@
3537
Carousel.DEFAULTS = {
3638
interval: 5000,
3739
pause: 'hover',
38-
wrap: true
40+
wrap: true,
41+
keyboard: true
3942
}
4043

4144
Carousel.prototype.keydown = function (e) {

js/tests/unit/carousel.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,85 @@ $(function () {
399399
strictEqual($template.find('.item')[1], $template.find('.active')[0], 'second item active')
400400
})
401401

402+
test('should go to previous item if left arrow key is pressed', function () {
403+
var templateHTML = '<div id="myCarousel" class="carousel" data-interval="false">'
404+
+ '<div class="carousel-inner">'
405+
+ '<div id="first" class="item">'
406+
+ '<img alt="">'
407+
+ '</div>'
408+
+ '<div id="second" class="item active">'
409+
+ '<img alt="">'
410+
+ '</div>'
411+
+ '<div id="third" class="item">'
412+
+ '<img alt="">'
413+
+ '</div>'
414+
+ '</div>'
415+
+ '</div>'
416+
var $template = $(templateHTML)
417+
418+
$template.bootstrapCarousel()
419+
420+
strictEqual($template.find('.item')[1], $template.find('.active')[0], 'second item active')
421+
422+
$template.trigger($.Event('keydown', { which: 37 }))
423+
424+
strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item active')
425+
})
426+
427+
test('should go to next item if right arrow key is pressed', function () {
428+
var templateHTML = '<div id="myCarousel" class="carousel" data-interval="false">'
429+
+ '<div class="carousel-inner">'
430+
+ '<div id="first" class="item active">'
431+
+ '<img alt="">'
432+
+ '</div>'
433+
+ '<div id="second" class="item">'
434+
+ '<img alt="">'
435+
+ '</div>'
436+
+ '<div id="third" class="item">'
437+
+ '<img alt="">'
438+
+ '</div>'
439+
+ '</div>'
440+
+ '</div>'
441+
var $template = $(templateHTML)
442+
443+
$template.bootstrapCarousel()
444+
445+
strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item active')
446+
447+
$template.trigger($.Event('keydown', { which: 39 }))
448+
449+
strictEqual($template.find('.item')[1], $template.find('.active')[0], 'second item active')
450+
})
451+
452+
test('should support disabling the keyboard navigation', function () {
453+
var templateHTML = '<div id="myCarousel" class="carousel" data-interval="false" data-keyboard="false">'
454+
+ '<div class="carousel-inner">'
455+
+ '<div id="first" class="item active">'
456+
+ '<img alt="">'
457+
+ '</div>'
458+
+ '<div id="second" class="item">'
459+
+ '<img alt="">'
460+
+ '</div>'
461+
+ '<div id="third" class="item">'
462+
+ '<img alt="">'
463+
+ '</div>'
464+
+ '</div>'
465+
+ '</div>'
466+
var $template = $(templateHTML)
467+
468+
$template.bootstrapCarousel()
469+
470+
strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item active')
471+
472+
$template.trigger($.Event('keydown', { which: 39 }))
473+
474+
strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item still active after right arrow press')
475+
476+
$template.trigger($.Event('keydown', { which: 37 }))
477+
478+
strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item still active after left arrow press')
479+
})
480+
402481
test('should only add mouseenter and mouseleave listeners when not on mobile', function () {
403482
var isMobile = 'ontouchstart' in document.documentElement
404483
var templateHTML = '<div id="myCarousel" class="carousel" data-interval="false" data-pause="hover">'

0 commit comments

Comments
 (0)