Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center the Bootstrap Tooltip on an SVG?

I want to shift the tooltip a few pixels to the right, so the arrow is in the center of the cell where the cursor is (currently, it's positioned at (0,0), that is, top left). This is my code:

 $("rect.cell").tooltip({
    title: "hola",
    placement: "top"
  });

and an image: enter image description here

Ideally I'd like to do this using javascript, so I can update the number of pixels if I change the size of the cells.

like image 455
nachocab Avatar asked May 23 '12 21:05

nachocab


1 Answers

Here's a simple extension of nachocab's getPosition implementation that supports both regular HTML elements and SVG elements:

getPosition: function (inside) {
  var el = this.$element[0]
  var width, height

  if ('http://www.w3.org/2000/svg' === el.namespaceURI) {
      var bbox = el.getBBox()
      width = bbox.width
      height = bbox.height
  } else {
      width = el.offsetWidth
      height = el.offsetHeight
  }

  return $.extend({}, (inside ? {top: 0, left: 0} : this.$element.offset()), {
    'width': width
  , 'height': height
  })
}
like image 79
Lars Grammel Avatar answered Sep 28 '22 06:09

Lars Grammel