I want to hide automatically the Bootstrap popovers after a few seconds. When the user hovers over a control, the popover must be displayed, but if the user doesn't move the mouse pointer, this popover must be hidden automatically after few seconds.
That is important because in a mobile phone or tablet when the user taps a control, the popover is displayed, and the focus remains on the same control while the user types something, with the popover hindering it.
To specify when the Popover should be shown and hidden, set the showEvent and hideEvent properties.
To make the image on hover in popover just insert an image as an HTML element to the data-mdb-content and set the data-mdb-html to true .
Note: This solution was written for Bootstrap 3.
This works, though there may be a more efficient method:
$('.pop').popover().click(function () {
setTimeout(function () {
$('.pop').popover('hide');
}, 2000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<span class="pop" data-original-title="My popover" data-content="Isn't it great?">Click me</span>
http://jsfiddle.net/isherwood/Bqq7C/27/
The accepted answer works just fine, but here's a more bootstrap approach:
Original answer
$('.pop').on('shown.bs.popover', function () {
var $pop = $(this);
setTimeout(function () {
$pop.popover('hide');
}, 2000);
});
Update from limplash
This answer misses one key information!! you have to add the trigger option while initializing popover .. {trigger:"manual"} .. otherwise the bootstraps attaches an onclick event to which causes the issue of two click required after first use .. following should is a proposed solution
$("#element").popover({ trigger:"manual" }).click(function() {
var pop = $(this);
pop.popover("show")
pop.on('shown.bs.popover',function() {
setTimeout(function() {
pop.popover("hide")}, 2200);
})
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With