diff --git a/docs/_includes/js/carousel.html b/docs/_includes/js/carousel.html
index b0665dba8db6..2b5b0111b13e 100644
--- a/docs/_includes/js/carousel.html
+++ b/docs/_includes/js/carousel.html
@@ -203,6 +203,11 @@
.carousel('next')
Events
Bootstrap's carousel class exposes two events for hooking into carousel functionality.
+ Both events have the following additional properties:
+
+ direction
: The direction in which the carousel is sliding (either "left"
or "right"
).
+ relatedTarget
: The DOM element that is being slid into place as the active item.
+
diff --git a/js/carousel.js b/js/carousel.js
index 9f063aec04c3..06e3f3731d76 100644
--- a/js/carousel.js
+++ b/js/carousel.js
@@ -59,7 +59,7 @@
if (pos > (this.$items.length - 1) || pos < 0) return
- if (this.sliding) return this.$element.one('slid.bs.carousel', function () { that.to(pos) }) // yes, "slid". not a typo. past tense of "to slide".
+ if (this.sliding) return this.$element.one('slid.bs.carousel', function () { that.to(pos) }) // yes, "slid"
if (activeIndex == pos) return this.pause().cycle()
return this.slide(pos > activeIndex ? 'next' : 'prev', $(this.$items[pos]))
@@ -103,9 +103,10 @@
if ($next.hasClass('active')) return this.sliding = false
- var e = $.Event('slide.bs.carousel', { relatedTarget: $next[0], direction: direction })
- this.$element.trigger(e)
- if (e.isDefaultPrevented()) return
+ var relatedTarget = $next[0]
+ var slideEvent = $.Event('slide.bs.carousel', { relatedTarget: relatedTarget, direction: direction })
+ this.$element.trigger(slideEvent)
+ if (slideEvent.isDefaultPrevented()) return
this.sliding = true
@@ -113,12 +114,13 @@
if (this.$indicators.length) {
this.$indicators.find('.active').removeClass('active')
- this.$element.one('slid.bs.carousel', function () { // yes, "slid". not a typo. past tense of "to slide".
+ this.$element.one('slid.bs.carousel', function () { // yes, "slid"
var $nextIndicator = $(that.$indicators.children()[that.getActiveIndex()])
$nextIndicator && $nextIndicator.addClass('active')
})
}
+ var slidEvent = $.Event('slid.bs.carousel', { relatedTarget: relatedTarget, direction: direction }) // yes, "slid"
if ($.support.transition && this.$element.hasClass('slide')) {
$next.addClass(type)
$next[0].offsetWidth // force reflow
@@ -129,14 +131,14 @@
$next.removeClass([type, direction].join(' ')).addClass('active')
$active.removeClass(['active', direction].join(' '))
that.sliding = false
- setTimeout(function () { that.$element.trigger('slid.bs.carousel') }, 0) // yes, "slid". not a typo. past tense of "to slide".
+ setTimeout(function () { that.$element.trigger(slidEvent) }, 0)
})
.emulateTransitionEnd($active.css('transition-duration').slice(0, -1) * 1000)
} else {
$active.removeClass('active')
$next.addClass('active')
this.sliding = false
- this.$element.trigger('slid.bs.carousel') // yes, "slid". not a typo. past tense of "to slide".
+ this.$element.trigger(slidEvent)
}
isCycling && this.cycle()
diff --git a/js/tests/unit/carousel.js b/js/tests/unit/carousel.js
index 065354451f0f..a4008bb0c773 100644
--- a/js/tests/unit/carousel.js
+++ b/js/tests/unit/carousel.js
@@ -66,6 +66,18 @@ $(function () {
}).carousel('next')
})
+ test('should fire slid event with direction', function () {
+ var template = '{{_i}}First Thumbnail label{{/i}}
Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.
{{_i}}Second Thumbnail label{{/i}}
Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.
{{_i}}Third Thumbnail label{{/i}}
Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.
‹› '
+ $.support.transition = false
+ stop()
+ $(template).on('slid.bs.carousel', function (e) {
+ e.preventDefault()
+ ok(e.direction)
+ ok(e.direction === 'right' || e.direction === 'left')
+ start()
+ }).carousel('next')
+ })
+
test('should fire slide event with relatedTarget', function () {
var template = '{{_i}}First Thumbnail label{{/i}}
Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.
{{_i}}Second Thumbnail label{{/i}}
Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.
{{_i}}Third Thumbnail label{{/i}}
Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.
‹› '
$.support.transition = false
@@ -80,6 +92,20 @@ $(function () {
.carousel('next')
})
+ test('should fire slid event with relatedTarget', function () {
+ var template = '{{_i}}First Thumbnail label{{/i}}
Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.
{{_i}}Second Thumbnail label{{/i}}
Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.
{{_i}}Third Thumbnail label{{/i}}
Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.
‹› '
+ $.support.transition = false
+ stop()
+ $(template)
+ .on('slid.bs.carousel', function (e) {
+ e.preventDefault()
+ ok(e.relatedTarget)
+ ok($(e.relatedTarget).hasClass('item'))
+ start()
+ })
+ .carousel('next')
+ })
+
test('should set interval from data attribute', 4, function () {
var template = $(' {{_i}}First Thumbnail label{{/i}}
Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.
{{_i}}Second Thumbnail label{{/i}}
Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.
{{_i}}Third Thumbnail label{{/i}}
Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.
‹ › ')
template.attr('data-interval', 1814)