0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-02-06 04:08:22 +01:00

Variable transition durations (#25662)

This commit is contained in:
Martijn Cuppens 2018-03-13 09:59:20 +01:00 committed by Johann-S
parent 1859595cb6
commit 1fadad1c33
11 changed files with 168 additions and 67 deletions

View File

@ -327,4 +327,4 @@ $('#myCarousel').on('slide.bs.carousel', function () {
### Change transition duration ### Change transition duration
The transition duration of `.carousel-item` can be changed with the `$carousel-transition` Sass variable before compiling or custom styles if you're using the compiled CSS. If multiple transitions are applied, make sure the transform transition is defined first (eg. `transition: transform 2s ease, opacity .5s ease-out`). The transition duration must be the same for each carousel item. The transition duration of `.carousel-item` can be changed with the `$carousel-transition` Sass variable before compiling or custom styles if you're using the compiled CSS. If multiple transitions are applied, make sure the transform transition is defined first (eg. `transition: transform 2s ease, opacity .5s ease-out`).

View File

@ -21,7 +21,6 @@ const Alert = (($) => {
const EVENT_KEY = `.${DATA_KEY}` const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api' const DATA_API_KEY = '.data-api'
const JQUERY_NO_CONFLICT = $.fn[NAME] const JQUERY_NO_CONFLICT = $.fn[NAME]
const TRANSITION_DURATION = 150
const Selector = { const Selector = {
DISMISS : '[data-dismiss="alert"]' DISMISS : '[data-dismiss="alert"]'
@ -109,9 +108,11 @@ const Alert = (($) => {
return return
} }
const transitionDuration = Util.getTransitionDurationFromElement(element)
$(element) $(element)
.one(Util.TRANSITION_END, (event) => this._destroyElement(element, event)) .one(Util.TRANSITION_END, (event) => this._destroyElement(element, event))
.emulateTransitionEnd(TRANSITION_DURATION) .emulateTransitionEnd(transitionDuration)
} }
_destroyElement(element) { _destroyElement(element) {

View File

@ -24,7 +24,6 @@ const Carousel = (($) => {
const ARROW_LEFT_KEYCODE = 37 // KeyboardEvent.which value for left arrow key const ARROW_LEFT_KEYCODE = 37 // KeyboardEvent.which value for left arrow key
const ARROW_RIGHT_KEYCODE = 39 // KeyboardEvent.which value for right arrow key const ARROW_RIGHT_KEYCODE = 39 // KeyboardEvent.which value for right arrow key
const TOUCHEVENT_COMPAT_WAIT = 500 // Time for mouse compat events to fire after touch const TOUCHEVENT_COMPAT_WAIT = 500 // Time for mouse compat events to fire after touch
const MILLISECONDS_MULTIPLIER = 1000
const Default = { const Default = {
interval : 5000, interval : 5000,
@ -102,8 +101,6 @@ const Carousel = (($) => {
this._element = $(element)[0] this._element = $(element)[0]
this._indicatorsElement = $(this._element).find(Selector.INDICATORS)[0] this._indicatorsElement = $(this._element).find(Selector.INDICATORS)[0]
this._transitionDuration = this._getTransitionDuration()
this._addEventListeners() this._addEventListeners()
} }
@ -225,24 +222,6 @@ const Carousel = (($) => {
return config return config
} }
_getTransitionDuration() {
// Get transition-duration of first element in the carousel
let transitionDuration = $(this._element).find(Selector.ITEM).css('transition-duration')
// Return 0 carousel item is not found
if (!transitionDuration) {
return 0
}
// If multiple durations are defined, take the first
transitionDuration = transitionDuration.split(',')[0]
// Multiply by 1000 if transition-duration is defined in seconds
return transitionDuration.indexOf('ms') > -1
? parseFloat(transitionDuration)
: parseFloat(transitionDuration) * MILLISECONDS_MULTIPLIER
}
_addEventListeners() { _addEventListeners() {
if (this._config.keyboard) { if (this._config.keyboard) {
$(this._element) $(this._element)
@ -406,6 +385,8 @@ const Carousel = (($) => {
$(activeElement).addClass(directionalClassName) $(activeElement).addClass(directionalClassName)
$(nextElement).addClass(directionalClassName) $(nextElement).addClass(directionalClassName)
const transitionDuration = Util.getTransitionDurationFromElement(activeElement)
$(activeElement) $(activeElement)
.one(Util.TRANSITION_END, () => { .one(Util.TRANSITION_END, () => {
$(nextElement) $(nextElement)
@ -418,7 +399,7 @@ const Carousel = (($) => {
setTimeout(() => $(this._element).trigger(slidEvent), 0) setTimeout(() => $(this._element).trigger(slidEvent), 0)
}) })
.emulateTransitionEnd(this._transitionDuration) .emulateTransitionEnd(transitionDuration)
} else { } else {
$(activeElement).removeClass(ClassName.ACTIVE) $(activeElement).removeClass(ClassName.ACTIVE)
$(nextElement).addClass(ClassName.ACTIVE) $(nextElement).addClass(ClassName.ACTIVE)

View File

@ -21,7 +21,6 @@ const Collapse = (($) => {
const EVENT_KEY = `.${DATA_KEY}` const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api' const DATA_API_KEY = '.data-api'
const JQUERY_NO_CONFLICT = $.fn[NAME] const JQUERY_NO_CONFLICT = $.fn[NAME]
const TRANSITION_DURATION = 600
const Default = { const Default = {
toggle : true, toggle : true,
@ -190,10 +189,11 @@ const Collapse = (($) => {
const capitalizedDimension = dimension[0].toUpperCase() + dimension.slice(1) const capitalizedDimension = dimension[0].toUpperCase() + dimension.slice(1)
const scrollSize = `scroll${capitalizedDimension}` const scrollSize = `scroll${capitalizedDimension}`
const transitionDuration = Util.getTransitionDurationFromElement(this._element)
$(this._element) $(this._element)
.one(Util.TRANSITION_END, complete) .one(Util.TRANSITION_END, complete)
.emulateTransitionEnd(TRANSITION_DURATION) .emulateTransitionEnd(transitionDuration)
this._element.style[dimension] = `${this._element[scrollSize]}px` this._element.style[dimension] = `${this._element[scrollSize]}px`
} }
@ -252,9 +252,11 @@ const Collapse = (($) => {
return return
} }
const transitionDuration = Util.getTransitionDurationFromElement(this._element)
$(this._element) $(this._element)
.one(Util.TRANSITION_END, complete) .one(Util.TRANSITION_END, complete)
.emulateTransitionEnd(TRANSITION_DURATION) .emulateTransitionEnd(transitionDuration)
} }
setTransitioning(isTransitioning) { setTransitioning(isTransitioning) {

View File

@ -21,8 +21,6 @@ const Modal = (($) => {
const EVENT_KEY = `.${DATA_KEY}` const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api' const DATA_API_KEY = '.data-api'
const JQUERY_NO_CONFLICT = $.fn[NAME] const JQUERY_NO_CONFLICT = $.fn[NAME]
const TRANSITION_DURATION = 300
const BACKDROP_TRANSITION_DURATION = 150
const ESCAPE_KEYCODE = 27 // KeyboardEvent.which value for Escape (Esc) key const ESCAPE_KEYCODE = 27 // KeyboardEvent.which value for Escape (Esc) key
const Default = { const Default = {
@ -187,10 +185,13 @@ const Modal = (($) => {
$(this._element).off(Event.CLICK_DISMISS) $(this._element).off(Event.CLICK_DISMISS)
$(this._dialog).off(Event.MOUSEDOWN_DISMISS) $(this._dialog).off(Event.MOUSEDOWN_DISMISS)
if (transition) { if (transition) {
const transitionDuration = Util.getTransitionDurationFromElement(this._element)
$(this._element) $(this._element)
.one(Util.TRANSITION_END, (event) => this._hideModal(event)) .one(Util.TRANSITION_END, (event) => this._hideModal(event))
.emulateTransitionEnd(TRANSITION_DURATION) .emulateTransitionEnd(transitionDuration)
} else { } else {
this._hideModal() this._hideModal()
} }
@ -263,9 +264,11 @@ const Modal = (($) => {
} }
if (transition) { if (transition) {
const transitionDuration = Util.getTransitionDurationFromElement(this._element)
$(this._dialog) $(this._dialog)
.one(Util.TRANSITION_END, transitionComplete) .one(Util.TRANSITION_END, transitionComplete)
.emulateTransitionEnd(TRANSITION_DURATION) .emulateTransitionEnd(transitionDuration)
} else { } else {
transitionComplete() transitionComplete()
} }
@ -369,9 +372,11 @@ const Modal = (($) => {
return return
} }
const backdropTransitionDuration = Util.getTransitionDurationFromElement(this._backdrop)
$(this._backdrop) $(this._backdrop)
.one(Util.TRANSITION_END, callback) .one(Util.TRANSITION_END, callback)
.emulateTransitionEnd(BACKDROP_TRANSITION_DURATION) .emulateTransitionEnd(backdropTransitionDuration)
} else if (!this._isShown && this._backdrop) { } else if (!this._isShown && this._backdrop) {
$(this._backdrop).removeClass(ClassName.SHOW) $(this._backdrop).removeClass(ClassName.SHOW)
@ -384,9 +389,11 @@ const Modal = (($) => {
if (Util.supportsTransitionEnd() && if (Util.supportsTransitionEnd() &&
$(this._element).hasClass(ClassName.FADE)) { $(this._element).hasClass(ClassName.FADE)) {
const backdropTransitionDuration = Util.getTransitionDurationFromElement(this._backdrop)
$(this._backdrop) $(this._backdrop)
.one(Util.TRANSITION_END, callbackRemove) .one(Util.TRANSITION_END, callbackRemove)
.emulateTransitionEnd(BACKDROP_TRANSITION_DURATION) .emulateTransitionEnd(backdropTransitionDuration)
} else { } else {
callbackRemove() callbackRemove()
} }

View File

@ -21,7 +21,6 @@ const Tab = (($) => {
const EVENT_KEY = `.${DATA_KEY}` const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api' const DATA_API_KEY = '.data-api'
const JQUERY_NO_CONFLICT = $.fn[NAME] const JQUERY_NO_CONFLICT = $.fn[NAME]
const TRANSITION_DURATION = 150
const Event = { const Event = {
HIDE : `hide${EVENT_KEY}`, HIDE : `hide${EVENT_KEY}`,
@ -162,9 +161,11 @@ const Tab = (($) => {
) )
if (active && isTransitioning) { if (active && isTransitioning) {
const transitionDuration = Util.getTransitionDurationFromElement(active)
$(active) $(active)
.one(Util.TRANSITION_END, complete) .one(Util.TRANSITION_END, complete)
.emulateTransitionEnd(TRANSITION_DURATION) .emulateTransitionEnd(transitionDuration)
} else { } else {
complete() complete()
} }

View File

@ -21,7 +21,6 @@ const Tooltip = (($) => {
const DATA_KEY = 'bs.tooltip' const DATA_KEY = 'bs.tooltip'
const EVENT_KEY = `.${DATA_KEY}` const EVENT_KEY = `.${DATA_KEY}`
const JQUERY_NO_CONFLICT = $.fn[NAME] const JQUERY_NO_CONFLICT = $.fn[NAME]
const TRANSITION_DURATION = 150
const CLASS_PREFIX = 'bs-tooltip' const CLASS_PREFIX = 'bs-tooltip'
const BSCLS_PREFIX_REGEX = new RegExp(`(^|\\s)${CLASS_PREFIX}\\S+`, 'g') const BSCLS_PREFIX_REGEX = new RegExp(`(^|\\s)${CLASS_PREFIX}\\S+`, 'g')
@ -335,9 +334,11 @@ const Tooltip = (($) => {
} }
if (Util.supportsTransitionEnd() && $(this.tip).hasClass(ClassName.FADE)) { if (Util.supportsTransitionEnd() && $(this.tip).hasClass(ClassName.FADE)) {
const transitionDuration = Util.getTransitionDurationFromElement(this.tip)
$(this.tip) $(this.tip)
.one(Util.TRANSITION_END, complete) .one(Util.TRANSITION_END, complete)
.emulateTransitionEnd(Tooltip._TRANSITION_DURATION) .emulateTransitionEnd(transitionDuration)
} else { } else {
complete() complete()
} }
@ -384,9 +385,11 @@ const Tooltip = (($) => {
if (Util.supportsTransitionEnd() && if (Util.supportsTransitionEnd() &&
$(this.tip).hasClass(ClassName.FADE)) { $(this.tip).hasClass(ClassName.FADE)) {
const transitionDuration = Util.getTransitionDurationFromElement(tip)
$(tip) $(tip)
.one(Util.TRANSITION_END, complete) .one(Util.TRANSITION_END, complete)
.emulateTransitionEnd(TRANSITION_DURATION) .emulateTransitionEnd(transitionDuration)
} else { } else {
complete() complete()
} }

View File

@ -17,6 +17,7 @@ const Util = (($) => {
let transition = false let transition = false
const MAX_UID = 1000000 const MAX_UID = 1000000
const MILLISECONDS_MULTIPLIER = 1000
// Shoutout AngusCroll (https://goo.gl/pxwQGp) // Shoutout AngusCroll (https://goo.gl/pxwQGp)
function toType(obj) { function toType(obj) {
@ -104,6 +105,23 @@ const Util = (($) => {
} }
}, },
getTransitionDurationFromElement(element) {
// Get transition-duration of the element
let transitionDuration = $(element).css('transition-duration')
// Return 0 if element or transition duration is not found
if (!transitionDuration) {
return 0
}
// If multiple durations are defined, take the first
transitionDuration = transitionDuration.split(',')[0]
// jQuery always converts transition durations into seconds,
// so multiply by 1000
return parseFloat(transitionDuration) * MILLISECONDS_MULTIPLIER
},
reflow(element) { reflow(element) {
return element.offsetHeight return element.offsetHeight
}, },

View File

@ -42,6 +42,50 @@ $(function () {
assert.strictEqual(typeof Util.isElement({}) === 'undefined', true) assert.strictEqual(typeof Util.isElement({}) === 'undefined', true)
}) })
QUnit.test('Util.getTransitionDurationFromElement should accept transition durations in milliseconds', function (assert) {
assert.expect(1)
var $div = $('<div style="transition: all 300ms ease-out;"></div>').appendTo($('#qunit-fixture'))
assert.strictEqual(Util.getTransitionDurationFromElement($div), 300)
})
QUnit.test('Util.getTransitionDurationFromElement should accept transition durations in seconds', function (assert) {
assert.expect(1)
var $div = $('<div style="transition: all .4s ease-out;"></div>').appendTo($('#qunit-fixture'))
assert.strictEqual(Util.getTransitionDurationFromElement($div), 400)
})
QUnit.test('Util.getTransitionDurationFromElement should get the first transition duration if multiple transition durations are defined', function (assert) {
assert.expect(1)
var $div = $('<div style="transition: transform .3s ease-out, opacity .2s;"></div>').appendTo($('#qunit-fixture'))
assert.strictEqual(Util.getTransitionDurationFromElement($div), 300)
})
QUnit.test('Util.getTransitionDurationFromElement should return 0 if transition duration is not defined', function (assert) {
assert.expect(1)
var $div = $('<div></div>').appendTo($('#qunit-fixture'))
assert.strictEqual(Util.getTransitionDurationFromElement($div), 0)
})
QUnit.test('Util.getTransitionDurationFromElement should return 0 if element is not found in DOM', function (assert) {
assert.expect(1)
var $div = $('#fake-id')
assert.strictEqual(Util.getTransitionDurationFromElement($div), 0)
})
QUnit.test('Util.getTransitionDurationFromElement should properly handle inherited transition durations', function (assert) {
assert.expect(1)
var $parent = $('<div style="transition-duration: 5s;"></div>')
var $child = $('<div style="transition-duration: inherit;"></div>')
$('#qunit-fixture').append($parent.append($child))
assert.strictEqual(Util.getTransitionDurationFromElement($child), 5000)
})
QUnit.test('Util.getUID should generate a new id uniq', function (assert) { QUnit.test('Util.getUID should generate a new id uniq', function (assert) {
assert.expect(2) assert.expect(2)
var id = Util.getUID('test') var id = Util.getUID('test')

View File

@ -42,6 +42,13 @@
<button type="button" class="btn btn-primary">Or do this</button> <button type="button" class="btn btn-primary">Or do this</button>
</p> </p>
</div> </div>
<div class="alert alert-warning alert-dismissible fade show" role="alert" style="transition-duration: 5s;">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
This alert will take 5 seconds to fade out.
</div>
</div> </div>
<script src="../../../assets/js/vendor/jquery-slim.min.js"></script> <script src="../../../assets/js/vendor/jquery-slim.min.js"></script>

View File

@ -147,6 +147,26 @@
</div> </div>
</div> </div>
<div class="modal fade" id="slowModal" tabindex="-1" role="dialog" aria-labelledby="slowModalLabel" aria-hidden="true" style="transition-duration: 5s;">
<div class="modal-dialog" role="document" style="transition-duration: inherit;">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<h4 class="modal-title" id="slowModalLabel">Lorem slowly</h4>
</div>
<div class="modal-body">
<p>Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Donec sed odio dui. Nullam quis risus eget urna mollis ornare vel eu leo. Nulla vitae elit libero, a pharetra augue.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal"> <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
Launch demo modal Launch demo modal
</button> </button>
@ -164,6 +184,12 @@
<br><br> <br><br>
<button type="button" class="btn btn-secondary btn-lg" data-toggle="modal" data-target="#slowModal">
Launch modal with slow transition
</button>
<br><br>
<div class="bg-dark text-white p-2" id="tall" style="display: none;"> <div class="bg-dark text-white p-2" id="tall" style="display: none;">
Tall body content to force the page to have a scrollbar. Tall body content to force the page to have a scrollbar.
</div> </div>
@ -225,6 +251,17 @@
}) })
.modal('show') .modal('show')
}) })
// Test transition duration
var t0, t1;
$('#slowModal').on('shown.bs.modal', function(){
t1 = performance.now()
console.log('transition-duration took ' + (t1 - t0) + 'ms.')
}).on('show.bs.modal', function(){
t0 = performance.now()
})
}) })
</script> </script>
</body> </body>