mirror of
https://github.com/twbs/bootstrap.git
synced 2025-02-26 23:54:23 +01:00
Carousel - Add attributes from and to for Slid and Slide events (#21668)
Carousel - Add attributes from and to for Slid and Slide events
This commit is contained in:
parent
78fc4d23fc
commit
c72a315740
@ -265,6 +265,8 @@ Bootstrap's carousel class exposes two events for hooking into carousel function
|
|||||||
|
|
||||||
- `direction`: The direction in which the carousel is sliding (either `"left"` or `"right"`).
|
- `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.
|
- `relatedTarget`: The DOM element that is being slid into place as the active item.
|
||||||
|
- `from`: The index of the current item
|
||||||
|
- `to`: The index of the next item
|
||||||
|
|
||||||
All carousel events are fired at the carousel itself (i.e. at the `<div class="carousel">`).
|
All carousel events are fired at the carousel itself (i.e. at the `<div class="carousel">`).
|
||||||
|
|
||||||
|
@ -282,9 +282,13 @@ const Carousel = (($) => {
|
|||||||
|
|
||||||
|
|
||||||
_triggerSlideEvent(relatedTarget, eventDirectionName) {
|
_triggerSlideEvent(relatedTarget, eventDirectionName) {
|
||||||
|
const targetIndex = this._getItemIndex(relatedTarget)
|
||||||
|
const fromIndex = this._getItemIndex($(this._element).find(Selector.ACTIVE_ITEM)[0])
|
||||||
const slideEvent = $.Event(Event.SLIDE, {
|
const slideEvent = $.Event(Event.SLIDE, {
|
||||||
relatedTarget,
|
relatedTarget,
|
||||||
direction: eventDirectionName
|
direction: eventDirectionName,
|
||||||
|
from: fromIndex,
|
||||||
|
to: targetIndex
|
||||||
})
|
})
|
||||||
|
|
||||||
$(this._element).trigger(slideEvent)
|
$(this._element).trigger(slideEvent)
|
||||||
@ -310,9 +314,10 @@ const Carousel = (($) => {
|
|||||||
|
|
||||||
_slide(direction, element) {
|
_slide(direction, element) {
|
||||||
const activeElement = $(this._element).find(Selector.ACTIVE_ITEM)[0]
|
const activeElement = $(this._element).find(Selector.ACTIVE_ITEM)[0]
|
||||||
|
const activeElementIndex = this._getItemIndex(activeElement)
|
||||||
const nextElement = element || activeElement &&
|
const nextElement = element || activeElement &&
|
||||||
this._getItemByDirection(direction, activeElement)
|
this._getItemByDirection(direction, activeElement)
|
||||||
|
const nextElementIndex = this._getItemIndex(nextElement)
|
||||||
const isCycling = Boolean(this._interval)
|
const isCycling = Boolean(this._interval)
|
||||||
|
|
||||||
let directionalClassName
|
let directionalClassName
|
||||||
@ -354,7 +359,9 @@ const Carousel = (($) => {
|
|||||||
|
|
||||||
const slidEvent = $.Event(Event.SLID, {
|
const slidEvent = $.Event(Event.SLID, {
|
||||||
relatedTarget: nextElement,
|
relatedTarget: nextElement,
|
||||||
direction: eventDirectionName
|
direction: eventDirectionName,
|
||||||
|
from: activeElementIndex,
|
||||||
|
to: nextElementIndex
|
||||||
})
|
})
|
||||||
|
|
||||||
if (Util.supportsTransitionEnd() &&
|
if (Util.supportsTransitionEnd() &&
|
||||||
|
@ -341,6 +341,49 @@ $(function () {
|
|||||||
.bootstrapCarousel('next')
|
.bootstrapCarousel('next')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
QUnit.test('should fire slid and slide events with from and to', function (assert) {
|
||||||
|
assert.expect(4)
|
||||||
|
var template = '<div id="myCarousel" class="carousel slide">'
|
||||||
|
+ '<div class="carousel-inner">'
|
||||||
|
+ '<div class="carousel-item active">'
|
||||||
|
+ '<img alt="">'
|
||||||
|
+ '<div class="carousel-caption">'
|
||||||
|
+ '<h4>First Thumbnail label</h4>'
|
||||||
|
+ '</div>'
|
||||||
|
+ '</div>'
|
||||||
|
+ '<div class="carousel-item">'
|
||||||
|
+ '<img alt="">'
|
||||||
|
+ '<div class="carousel-caption">'
|
||||||
|
+ '<h4>Second Thumbnail label</h4>'
|
||||||
|
+ '</div>'
|
||||||
|
+ '</div>'
|
||||||
|
+ '<div class="carousel-item">'
|
||||||
|
+ '<img alt="">'
|
||||||
|
+ '<div class="carousel-caption">'
|
||||||
|
+ '<h4>Third Thumbnail label</h4>'
|
||||||
|
+ '</div>'
|
||||||
|
+ '</div>'
|
||||||
|
+ '</div>'
|
||||||
|
+ '<a class="left carousel-control" href="#myCarousel" data-slide="prev">‹</a>'
|
||||||
|
+ '<a class="right carousel-control" href="#myCarousel" data-slide="next">›</a>'
|
||||||
|
+ '</div>'
|
||||||
|
|
||||||
|
var done = assert.async()
|
||||||
|
$(template)
|
||||||
|
.on('slid.bs.carousel', function (e) {
|
||||||
|
assert.ok(e.from !== undefined, 'from present')
|
||||||
|
assert.ok(e.to !== undefined, 'to present')
|
||||||
|
$(this).off()
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
.on('slide.bs.carousel', function (e) {
|
||||||
|
assert.ok(e.from !== undefined, 'from present')
|
||||||
|
assert.ok(e.to !== undefined, 'to present')
|
||||||
|
$(this).off('slide.bs.carousel')
|
||||||
|
})
|
||||||
|
.bootstrapCarousel('next')
|
||||||
|
})
|
||||||
|
|
||||||
QUnit.test('should set interval from data attribute', function (assert) {
|
QUnit.test('should set interval from data attribute', function (assert) {
|
||||||
assert.expect(4)
|
assert.expect(4)
|
||||||
var templateHTML = '<div id="myCarousel" class="carousel slide">'
|
var templateHTML = '<div id="myCarousel" class="carousel slide">'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user