Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap tooltip wrong position on svg element when page is scrolled down

I have problem with Bootstrap (3.3.7) tooltips for svg elements. Tooltips working well when page is scrolled to the top. But whenever i scroll page down, tooltips have wrong vertical position (the more i scroll down the page, the distance between tootltip and tooltiped element is larger).

Here is a basic example with mentioned problem (there is also test button with tooltip which working well):

HTML:

<div class="text-center">
  <div class="top-wrap"></div>
  <div class="svg-wrap">
    <svg height="100" width="100">
      <circle id="svg-el" cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
      Sorry, your browser does not support inline SVG.  
    </svg> 
  </div>

  <button id="test-button" type="button" class="btn btn-default">Tooltip on left</button>
</div>

jQuery:

$(function(){
    $('#svg-el').tooltip({
        'container': '.svg-wrap',
        'placement': 'top',
        'html':'true',
        'title': '<strong class="text-uppercase">France</strong><br/> example html content.'
    });
  $('#test-button').tooltip({
        'container': 'body',
        'placement': 'top',
        'title': 'Example tooltip.'
    });
});

See working jsfiddle .

Can someone help me or have someone the same problem? I tried many options for tooltip but I'm not able to solve the problem.

like image 244
user1827257 Avatar asked Feb 18 '17 12:02

user1827257


People also ask

How do I change the tooltip position in bootstrap?

So to position a tooltip as your requirement just add data-bs-placement=”top/right/left/bottom” attribute to your <a> or <button> tag to change tooltip position.

How do you set a tooltip position?

Basic Tooltip The tooltip text is placed inside an inline element (like <span>) with class="tooltiptext" . CSS: The tooltip class use position:relative , which is needed to position the tooltip text ( position:absolute ). Note: See examples below on how to position the tooltip.

How to use tooltip in Bootstrap?

To create a tooltip, add the data-toggle="tooltip" attribute to an element. Note: Tooltips must be initialized with jQuery: select the specified element and call the tooltip() method.


1 Answers

To elaborate on @gabel's answer, for fixing bootstrap v3.3.7,

  • go to https://github.com/twbs/bootstrap/blob/v3.3.7/js/tooltip.js
  • look for the declaration of Tooltip.getPosition: Tooltip.prototype.getPosition = function ($element) {

  • The offending lines are: var isSvg = window.SVGElement && el instanceof window.SVGElement and var elOffset = isBody ? { top: 0, left: 0 } : (isSvg ? null : $element.offset())

As you can see, the positioning is deliberately disabled for SVGs. So enable it, by forcibly setting isSvg to false: var isSvg = false

If you're working with the minified version of bootstrap, it is relatively easy to search & replace by looking for getPosition's prototype declaration as well. The isSvg variable is obfuscated, but should be around there as well.

like image 189
Cardin Avatar answered Nov 24 '22 19:11

Cardin