Reputation: 14717
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.
Upvotes: 5
Views: 13179
Reputation: 2968
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.
Upvotes: 4
Reputation: 944
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>
Upvotes: 8
Reputation: 944
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>
Upvotes: 2