简体   繁体   English

Twitter引导程序2.3.2 popover在悬停时保持打开状态

[英]Twitter bootstrap 2.3.2 popover stay open while hovering

I have a bottom-oriented popover that I'd like to be a bit more forgiving than the default popover, which vanishes as soon as the mouse leaves the trigger. 我有一个底部导向的弹出窗口,我想比默认弹出窗口更宽容,一旦鼠标离开触发器就会消失。

$('#example').popover({
  html: true,
  trigger: 'hover',
  container: '#example',
  placement: 'bottom',
  content: function () {
      return '<div class="box">here is some content</div>';
  }
});

I've got it to stay open as long as the mouse is over the trigger or the popover content, but that's tough for the user, since they've got to mouse from the trigger element to the arrow to the content without leaving those areas in order to interact with the popover. 只要鼠标悬停在触发器或弹出窗口内容上,我就可以保持打开状态,但这对用户来说很难,因为他们必须将鼠标从触发元素移动到箭头到内容而不离开这些区域为了与popover互动。 Two solutions in mind, neither is working: 考虑两种解决方案,两种解决方案都不起

1) the delay option ought to do this. 1)延迟选项应该这样做。 adding delay: {hide: 500} to the popover call leaves the popover open after the mouse leaves, but re-entering the trigger elem or the popover before it disappears does not tell bootstrap to keep the popover open, so goes away at the end of the initial timeout. 添加delay: {hide: 500}到popover调用会在鼠标离开后离开popover,但是在它消失之前重新进入触发器elem或popover不会告诉bootstrap保持popover打开,所以最后消失初始超时

2) widen the arrow's containing element so that the mouse going from trigger element to background between trigger element and popover to popover works (the mouse then would never have left the trigger/element). 2)加宽箭头的包含元素,使鼠标从触发元素到触发元素和弹出窗口之间的背景弹出工作(鼠标永远不会离开触发器/元素)。 The following works except the arrow is drawn with overlapping CSS borders, so the background is not transparent: http://jsfiddle.net/HAZS8/ 除箭头之外的以下工作是使用重叠的CSS边框绘制的,因此背景不透明: http//jsfiddle.net/HAZS8/

.popover.bottom .arrow {
    left: 0%;
    padding-left:50%;
    padding-right:50%;
}

The workaround is to hard-wire the mouseover and mouseleave events with jquery, or to replace the overlapping-borders arrow with an image. 解决方法是使用jquery硬连接mouseover和mouseleave事件,或者用图像替换重叠边框箭头。 Better fixes? 更好的修复?

I have a more generic approach to solving this one, which I'm using myself. 我有一个更通用的方法来解决这个问题,我正在使用它。 It involves overloading the popover's hide function, checking if the associated tooltip is being hovered over, and reacts appropriately - rather than adding all that event handling & html5 data setting. 它涉及重载popover的hide函数,检查相关的工具提示是否正在悬停,并做出适当的反应 - 而不是添加所有事件处理和html5数据设置。

(function($) {

    var oldHide = $.fn.popover.Constructor.prototype.hide;

    $.fn.popover.Constructor.prototype.hide = function() {
        if (this.options.trigger === "hover" && this.tip().is(":hover")) {
            var that = this;
            // try again after what would have been the delay
            setTimeout(function() {
                return that.hide.call(that, arguments);
            }, that.options.delay.hide);
            return;
        }
        oldHide.call(this, arguments);
    };

})(jQuery);

Load this after your bootstrap & jQuery sources. 在bootstrap和jQuery源之后加载它。

You can handle the show and hide events for the popover: 您可以处理弹出窗口的showhide事件:

$('#example').popover({
    html: true,
    trigger: 'hover',
    container: '#example',
    placement: 'bottom',
    content: function () {
        return '<div class="box">here is some content</div>';
    },
    animation: false
}).on({
    show: function (e) {
        var $this = $(this);

        // Currently hovering popover
        $this.data("hoveringPopover", true);

        // If it's still waiting to determine if it can be hovered, don't allow other handlers
        if ($this.data("waitingForPopoverTO")) {
            e.stopImmediatePropagation();
        }
    },
    hide: function (e) {
        var $this = $(this);

        // If timeout was reached, allow hide to occur
        if ($this.data("forceHidePopover")) {
            $this.data("forceHidePopover", false);
            return true;
        }

        // Prevent other `hide` handlers from executing
        e.stopImmediatePropagation();

        // Reset timeout checker
        clearTimeout($this.data("popoverTO"));

        // No longer hovering popover
        $this.data("hoveringPopover", false);

        // Flag for `show` event
        $this.data("waitingForPopoverTO", true);

        // In 1500ms, check to see if the popover is still not being hovered
        $this.data("popoverTO", setTimeout(function () {
            // If not being hovered, force the hide
            if (!$this.data("hoveringPopover")) {
                $this.data("forceHidePopover", true);
                $this.data("waitingForPopoverTO", false);
                $this.popover("hide");
            }
        }, 1500));

        // Stop default behavior
        return false;
    }
});

DEMO: http://jsfiddle.net/L4Hc2/ 演示: http //jsfiddle.net/L4Hc2/

It doesn't seem like there's anything built-in for the popover for the functionality you want, so this is what I came up with :) 它似乎没有任何内置的popover功能你想要的功能,所以这就是我想出来的:)

What's nice is that it only allows handlers to execute if they really should - if the popover is actually being hidden or actually being shown. 有什么好处的,它只允许处理程序执行,如果他们确实应该 - 如果popover实际上被隐藏或实际显示。 Also, each instance of a popover is unique from each other, so there is no global trickery going on. 此外,每个弹出窗口的实例彼此是唯一的,因此没有全局欺骗。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM