aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter West <[email protected]>2014-06-03 16:02:06 +0100
committerHeinrich Fenkart <[email protected]>2014-09-13 04:25:34 +0200
commit4c9850701069954375b19e19bd1513654130eacf (patch)
tree7b71019c94ba99c73bbe7ac24962a80e1d30e247
parent0a4c39dc40e16a3aa18721bf78935e77ac11f097 (diff)
downloadbootstrap-4c9850701069954375b19e19bd1513654130eacf.tar.xz
bootstrap-4c9850701069954375b19e19bd1513654130eacf.zip
Correctly selects arrow placement for a tooltip
Fixes #13696. Fixes #13696. Fixes #14197. Closes #13718.
-rw-r--r--js/tests/unit/tooltip.js31
-rw-r--r--js/tooltip.js14
2 files changed, 39 insertions, 6 deletions
diff --git a/js/tests/unit/tooltip.js b/js/tests/unit/tooltip.js
index c0c628485..b578704af 100644
--- a/js/tests/unit/tooltip.js
+++ b/js/tests/unit/tooltip.js
@@ -836,4 +836,35 @@ $(function () {
equal(currentUid, $('#tt-content').text())
})
+ test('should position arrow correctly when tooltip is moved to not appear offscreen', function () {
+ stop()
+
+ var styles = '<style>'
+ + '.tooltip, .tooltip *, .tooltip *:before, .tooltip *:after { box-sizing: border-box; }'
+ + '.tooltip { position: absolute; }'
+ + '.tooltip-arrow { position: absolute; width: 0; height: 0; }'
+ + '.tooltip .tooltip-inner { max-width: 200px; padding: 3px 8px; }'
+ + '</style>'
+ var $styles = $(styles).appendTo('head')
+
+ $('<a href="#" title="tooltip title" style="position: absolute; bottom: 0; right: 0;">Foobar</a>')
+ .appendTo('body')
+ .on('shown.bs.tooltip', function () {
+ var arrowStyles = $(this).data('bs.tooltip').$tip.find('.tooltip-arrow').attr('style')
+ ok(/left/i.test(arrowStyles) && !/top/i.test(arrowStyles), 'arrow positioned correctly')
+ $(this).bootstrapTooltip('hide')
+ })
+ .on('hidden.bs.tooltip', function () {
+ $styles.remove()
+ $(this).remove()
+ start()
+ })
+ .bootstrapTooltip({
+ container: 'body',
+ placement: 'top',
+ trigger: 'manual'
+ })
+ .bootstrapTooltip('show')
+ })
+
})
diff --git a/js/tooltip.js b/js/tooltip.js
index 1e10af27f..614f41451 100644
--- a/js/tooltip.js
+++ b/js/tooltip.js
@@ -261,16 +261,18 @@
if (delta.left) offset.left += delta.left
else offset.top += delta.top
- var arrowDelta = delta.left ? delta.left * 2 - width + actualWidth : delta.top * 2 - height + actualHeight
- var arrowPosition = delta.left ? 'left' : 'top'
- var arrowOffsetPosition = delta.left ? 'offsetWidth' : 'offsetHeight'
+ var isVertical = /top|bottom/.test(placement)
+ var arrowDelta = isVertical ? delta.left * 2 - width + actualWidth : delta.top * 2 - height + actualHeight
+ var arrowOffsetPosition = isVertical ? 'offsetWidth' : 'offsetHeight'
$tip.offset(offset)
- this.replaceArrow(arrowDelta, $tip[0][arrowOffsetPosition], arrowPosition)
+ this.replaceArrow(arrowDelta, $tip[0][arrowOffsetPosition], isVertical)
}
- Tooltip.prototype.replaceArrow = function (delta, dimension, position) {
- this.arrow().css(position, delta ? (50 * (1 - delta / dimension) + '%') : '')
+ Tooltip.prototype.replaceArrow = function (delta, dimension, isHorizontal) {
+ this.arrow()
+ .css(isHorizontal ? 'left' : 'top', 50 * (1 - delta / dimension) + '%')
+ .css(isHorizontal ? 'top' : 'left', '')
}
Tooltip.prototype.setContent = function () {