如何在我的鼠标移动之前保持Twitter Bootstrap Popover打开?

Tin*_*ool 50 javascript jquery popover twitter-bootstrap twitter-bootstrap-3

我有一个链接,使用Twitter Bootstrap Popover版本1.3.0来显示一些信息.此信息包含一个链接,但每次我将鼠标从链接移动到弹出窗口时,弹出窗口就会消失.

如何让弹出窗口打开足够长的时间以使鼠标移动到其中?然后当鼠标移出链接并弹出时,隐藏它?

或者是否有其他插件可以做到这一点?

小智 31

使用bootstrap(使用版本2测试)我找到了以下代码:

$("a[rel=popover]")
            .popover({
                offset: 10,
                trigger: 'manual',
                animate: false,
                html: true,
                placement: 'left',
                template: '<div class="popover" onmouseover="$(this).mouseleave(function() {$(this).hide(); });"><div class="arrow"></div><div class="popover-inner"><h3 class="popover-title"></h3><div class="popover-content"><p></p></div></div></div>'

            }).click(function(e) {
                e.preventDefault() ;
            }).mouseenter(function(e) {
                $(this).popover('show');
            });
Run Code Online (Sandbox Code Playgroud)

重点是使用mouseleave()启用程序覆盖模板.我希望这有帮助.

  • 这对我有用.我希望这个功能内置得更好,所以你不必通过这个详细的'模板'选项."模板"选项甚至没有在http://twitter.github.com/bootstrap/javascript.html#popovers中记录. (2认同)

Kev*_*nce 26

只是为了添加Marchello的例子,如果你想让popover在用户将鼠标移离popover source链接时消失,试试这个.

var timeoutObj;
$('.nav_item a').popover({
    offset: 10,
    trigger: 'manual',
    html: true,
    placement: 'right',
    template: '<div class="popover" onmouseover="clearTimeout(timeoutObj);$(this).mouseleave(function() {$(this).hide();});"><div class="arrow"></div><div class="popover-inner"><h3 class="popover-title"></h3><div class="popover-content"><p></p></div></div></div>'
}).mouseenter(function(e) {
    $(this).popover('show');
}).mouseleave(function(e) {
    var ref = $(this);
    timeoutObj = setTimeout(function(){
        ref.popover('hide');
    }, 50);
});
Run Code Online (Sandbox Code Playgroud)

  • 当进入弹出窗口时,为什么得到错误说"timeoutObj未定义" (2认同)

Fiz*_*zix 26

Bootstrap 3及以上版本

简单,只需使用该container选项并将其作为调用popover的元素.这样,popover就是调用它的元素的子元素.因此,您在技术上仍然悬停在父级上,因为子弹出属于它.

例如:

HTML:

<div class="pop" data-content="Testing 12345">This has a popover</div>
<div class="pop" data-content="Testing 12345">This has a popover</div>
<div class="pop" data-content="Testing 12345">This has a popover</div>
Run Code Online (Sandbox Code Playgroud)

jQuery的:

$.each()在我的每个元素上运行一个循环,我想要一个绑定到其父级的弹出窗口.在这种情况下,每个元素都具有类pop.

$('.pop').each(function () {
    var $elem = $(this);
    $elem.popover({
        placement: 'top',
        trigger: 'hover',
        html: true,
        container: $elem
    });
});
Run Code Online (Sandbox Code Playgroud)

CSS:

此部分是可选的,但建议使用.它将弹出窗口向下移动7个像素,以便于访问.

.pop .popover {
    margin-top:7px;
}
Run Code Online (Sandbox Code Playgroud)

工作演示

  • 通过将其绑定到元素,popover将受元素的父级溢出规则的约束.意思是如果它超出边界并且溢出是除"可见"之外的任何东西,那么用户将需要滚动以查看弹出窗口,这是非常不愉快的.只要您的弹出窗口不大或父元素不会限制其溢出,此方法就可以了. (2认同)

cle*_*lem 19

这有点hacky,但建立marchello的例子,我做了这个(不需要模板):

$(".trigger-link").popover({
  trigger: "manual",
}).on("click", function(e) {
  e.preventDefault();
}).on("mouseenter", function() {
  var _this = this;
  $(this).popover("show");
  $(this).siblings(".popover").on("mouseleave", function() {
    $(_this).popover('hide');
  });
}).on("mouseleave", function() {
  var _this = this;
  setTimeout(function() {
    if (!$(".popover:hover").length) {
      $(_this).popover("hide")
    }
  }, 100);
});
Run Code Online (Sandbox Code Playgroud)

setTimeout有助于确保从触发链接到弹出窗口的时间.

  • 最好用`$(_ this).popover('hide')替换`$(this).hide();`;` (2认同)

ste*_*els 11

bootstrap github repo上的这个问题处理这个问题.胖指出实验"在上/下/左/右"的位置.它工作得很好,但你必须确保弹出触发器不是静态定位的css.否则,弹出框将不会出现在您想要的位置.

HTML:

<span class="myClass" data-content="lorem ipsum content" data-original-title="pop-title">Hover me to show a popover.</span>

CSS:

/*CSS */
.myClass{ position: relative;}
Run Code Online (Sandbox Code Playgroud)

JS:

$(function(){
  $('.myClass').popover({placement: 'in top'});
});  
Run Code Online (Sandbox Code Playgroud)

  • 如果您正在寻找一个包含更多自定义功能的bootstrap popover的替代方案,请查看http://stevenbenner.github.com/jquery-powertip/ (2认同)

小智 5

Bootstrap 3 的解决方案对我们有用。

var timeoutObj;
$('.list-group a').popover({
    offset: 10,
    trigger: 'manual',
    html: true,
    placement: 'right',
    template: '<div class="popover" onmouseover="$(this).mouseleave(function() {$(this).hide();});"><div class="arrow"></div><div class="popover-inner"><h3 class="popover-title"></h3><div class="popover-content"><p></p></div></div></div>'
}).mouseenter(function(e) {
    $(this).popover('show');
}).mouseleave(function(e) {
    var _this = this;
    setTimeout(function() {
        if (!$(".popover:hover").length) {
            $(_this).popover("hide");
        }
    }, 100);
}); 
Run Code Online (Sandbox Code Playgroud)


Tin*_*ool -2

现在我只是切换到 webuiPopover,它就可以工作了。