Skip to content

Commit 24ae068

Browse files
committed
Tooltip: Rely on getBoundingClientRect for width and height
Fixes twbs#14553.
1 parent e70fc61 commit 24ae068

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

js/tests/unit/tooltip.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,4 +919,42 @@ $(function () {
919919
.bootstrapTooltip('show')
920920
})
921921

922+
test('should correctly position tooltips on transformed elements', function () {
923+
var styleProps = document.documentElement.style
924+
if (!('transform' in styleProps) && !('webkitTransform' in styleProps) && !('msTransform' in styleProps)) {
925+
expect(0)
926+
return
927+
}
928+
929+
stop()
930+
931+
var styles = '<style>'
932+
+ '#qunit-fixture { top: 0; left: 0; }'
933+
+ '.tooltip, .tooltip *, .tooltip *:before, .tooltip *:after { box-sizing: border-box; }'
934+
+ '.tooltip { position: absolute; }'
935+
+ '.tooltip .tooltip-inner { width: 24px; height: 24px; font-family: Helvetica; }'
936+
+ '#target { position: absolute; top: 100px; left: 50px; width: 100px; height: 200px; -webkit-transform: rotate(270deg); -ms-transform: rotate(270deg); transform: rotate(270deg); }'
937+
+ '</style>'
938+
var $styles = $(styles).appendTo('head')
939+
940+
var $element = $('<div id="target" title="1"/>').appendTo('#qunit-fixture')
941+
942+
$element
943+
.on('shown.bs.tooltip', function () {
944+
var offset = $('.tooltip').offset()
945+
$styles.remove()
946+
ok(Math.abs(offset.left - 88) <= 1, 'tooltip has correct horizontal location')
947+
ok(Math.abs(offset.top - 126) <= 1, 'tooltip has correct vertical location')
948+
$element.bootstrapTooltip('hide')
949+
start()
950+
})
951+
.bootstrapTooltip({
952+
container: 'body',
953+
placement: 'top',
954+
trigger: 'manual'
955+
})
956+
957+
$element.bootstrapTooltip('show')
958+
})
959+
922960
})

js/tooltip.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,6 @@
329329

330330
var el = $element[0]
331331
var isBody = el.tagName == 'BODY'
332-
var isSvg = window.SVGElement && el instanceof window.SVGElement
333332

334333
var elRect = el.getBoundingClientRect()
335334
if (elRect.width == null) {
@@ -338,10 +337,7 @@
338337
}
339338
var elOffset = isBody ? { top: 0, left: 0 } : $element.offset()
340339
var scroll = { scroll: isBody ? document.documentElement.scrollTop || document.body.scrollTop : $element.scrollTop() }
341-
var outerDims = isSvg ? {} : {
342-
width: isBody ? $(window).width() : $element.outerWidth(),
343-
height: isBody ? $(window).height() : $element.outerHeight()
344-
}
340+
var outerDims = isBody ? { width: $(window).width(), height: $(window).height() } : null
345341

346342
return $.extend({}, elRect, scroll, outerDims, elOffset)
347343
}

0 commit comments

Comments
 (0)