5

I am using the tooltip tool in Bootstrap 3.x in my web app to show helpful info. It works in this way:

$('[data-toggle="tooltip"]').tooltip({
        'animation': true,
        'placement': 'top',
        'trigger': 'hover', 
        'html':true,
        'container': 'body'
});

Please note that I have to use hover to trigger the tooltip. Now I need to add a new tooltip and its tooltip text contains a HTML link. A visitor should be able to mouse over the tooltip text to click the link.

The problem is that when a visitor moves his mouse, the tooltip text disappears before he can reach the tooltip text area.

curious1
  • 14,155
  • 37
  • 130
  • 231

3 Answers3

7

This is using tooltip now. See if this better answer your question.

var originalLeave = $.fn.tooltip.Constructor.prototype.leave;
$.fn.tooltip.Constructor.prototype.leave = function(obj) {
  var self = obj instanceof this.constructor ?
    obj : $(obj.currentTarget)[this.type](this.getDelegateOptions()).data('bs.' + this.type)
  var container, timeout;

  originalLeave.call(this, obj);

  if (obj.currentTarget) {
    container = $(obj.currentTarget).siblings('.tooltip')
    timeout = self.timeout;
    container.one('mouseenter', function() {
      //We entered the actual popover – call off the dogs
      clearTimeout(timeout);
      //Let's monitor popover content instead
      container.one('mouseleave', function() {
        $.fn.tooltip.Constructor.prototype.leave.call(self, self);
      });
    })
  }
};


$('body').tooltip({
  selector: '[data-toggle]',
  trigger: 'click hover',
  placement: 'auto',
  delay: {
    show: 50,
    hide: 400
  }
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<p>
  <button class='btn btn-primary btn-large' data-toggle="tooltip" data-html="true" title="<a href='http://www.wojt.eu' target='blank' >click me, I'll try not to disappear</a>">hover here</button>
</p>
Monkey_Dev1400
  • 924
  • 6
  • 14
  • It works for my need. It would be ideal that it could work the way you did for popover. Thanks!!! – curious1 Jun 06 '17 at 20:42
  • Nice solution! Quick note that you'll need to instantiate with the delay options for it to work since they are not added by default. – Tim Vermaelen Jan 10 '18 at 09:41
4

This works for me on bootstrap 4.1.3 using documented events (no patching!)

$(document).ready(function() {
    $('body').on('inserted.bs.tooltip', function(e) {
        var $target = $(e.target);

        // Keep track so we can check if mouse is hovering over the tooltip
        $('[role="tooltip"]').hover( function() {
            $(this).toggleClass('hover');
        });

        $target.on('hide.bs.tooltip', function(e) {
            // If tooltip is under the mouse, prevent hide but
            // add handler to hide when mouse leaves tooltip
            if ($('[role="tooltip"]').hasClass('hover')) {
                $('[role="tooltip"]').on('mouseleave', function() {
                    setTimeout(function() {
                            $target.tooltip('hide');
                        }, yourHideDelay);
                });
                // Tell bootstrap tooltip to bail and not actually hide
                e.preventDefault();
                return;
            }
        });
    });
});

It is then possible to select text from the tooltip or click links within it etc.

sparrowt
  • 2,641
  • 25
  • 24
  • 2
    I used yours as a reference, but had to do it a bit differently to get it to work https://pastebin.com/Q21B57UG Thanks! – Gavin Feb 12 '20 at 14:48
2

Time gap might solve your problem.

var originalLeave = $.fn.popover.Constructor.prototype.leave;
$.fn.popover.Constructor.prototype.leave = function(obj){
  var self = obj instanceof this.constructor ?
    obj : $(obj.currentTarget)[this.type](this.getDelegateOptions()).data('bs.' + this.type)
  var container, timeout;

  originalLeave.call(this, obj);

  if(obj.currentTarget) {
    container = $(obj.currentTarget).siblings('.popover')
    timeout = self.timeout;
    container.one('mouseenter', function(){
      //We entered the actual popover – call off the dogs
      clearTimeout(timeout);
      //Let's monitor popover content instead
      container.one('mouseleave', function(){
        $.fn.popover.Constructor.prototype.leave.call(self, self);
      });
    })
  }
};


$('body').popover({ selector: '[data-popover]', trigger: 'click hover', placement: 'auto', delay: {show: 50, hide: 400}});

  
  
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<p id='container'>
<button class='btn btn-primary btn-large' data-popover="true" data-html=true data-content="<a href='http://www.wojt.eu' target='blank' >click me, I'll try not to disappear</a>">hover here</button>
</p>
Monkey_Dev1400
  • 924
  • 6
  • 14