Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap: hover over the tooltip text to click a link

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.

like image 581
curious1 Avatar asked Jun 06 '17 17:06

curious1


2 Answers

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>
like image 194
Monkey_Dev1400 Avatar answered Oct 06 '22 17:10

Monkey_Dev1400


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.

like image 45
sparrowt Avatar answered Oct 06 '22 18:10

sparrowt