How can I hold Twitter Bootstrap Popover open until my mouse moves into it?

前端 未结 16 1381
野性不改
野性不改 2020-12-04 05:55

I have a link that uses the Twitter Bootstrap Popover version 1.3.0 to show some information. This information includes a link, but every-time I move my mouse from the link

相关标签:
16条回答
  • 2020-12-04 06:30

    Finally I fix this problem. Popover disappear is because Popover not child node of link, it is child node of body.

    So fix it is easy, change bootstrap-twipsy.js content:

    change .prependTo(document.body) to .prependTo(this.$element)

    and fix position problem cause by change.

    and some use link tiger popover will cause popover with link too, so add a span contain link, so problem solved.

    0 讨论(0)
  • 2020-12-04 06:31

    With bootstrap (tested with version 2) I figured out the following code:

    $("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');
                });
    

    The main point is to override template with mouseleave() enabler. I hope this helps.

    0 讨论(0)
  • 2020-12-04 06:32

    I didn't like any of the answers I've found, so I combined some answers that were close to make the following code. It allows you to end up just typing $(selector).pinnablepopover(options); every time you want to make a 'pinnable' popover.

    Code that makes things easy:

    $.fn.popoverHoverShow = function ()
    {
        if(this.data('state') !== 'pinned')
        {
            if(!this.data('bs.popover').$tip || (this.data('bs.popover').$tip && this.data('bs.popover').$tip.is(':hidden')))
            {
                this.popover('show');
            }
        }
    };
    $.fn.popoverHoverHide = function ()
    {
        if (this.data('state') !== 'pinned')
        {
            var ref = this;
            this.data('bs.popover').$tip.data('timeout', setTimeout(function(){ ref.popover('hide') }, 100))
            .on('mouseenter', function(){ clearTimeout($(this).data('timeout')) })
            .on('mouseleave', function(){ $(this).data('timeout', setTimeout(function(){ ref.popover('hide') }, 100)) });
            this.on('mouseenter', function(){ clearTimeout($(this).data('timeout')) });
        }
    };
    $.fn.popoverClickToggle = function ()
    {
        if (this.data('state') !== 'pinned')
        {
            this.data('state', 'pinned');
        }
        else
        {
            this.data('state', 'hover')
        }
    };
    $.fn.pinnablepopover = function (options)
    {
        options.trigger = manual;
        this.popover(options)
        .on('mouseenter', function(){ $(this).popoverHoverShow() })
        .on('mouseleave', function(){ $(this).popoverHoverHide() })
        .on('click', function(){ $(this).popoverClickToggle() });
    };
    

    Example usage:

    $('[data-toggle=popover]').pinnablepopover({html: true, container: 'body'});
    
    0 讨论(0)
  • 2020-12-04 06:40

    This is a little hacky, but building off of marchello's example, I did this (no need for template):

    $(".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);
    });
    

    The setTimeout helps ensure that there's time to travel from the trigger link to the popover.

    0 讨论(0)
  • 2020-12-04 06:41

    Here is what i did:

    e = $("a[rel=popover]")
    e.popover({
        content: d, 
        html:true, 
        trigger:'hover',
        delay: {hide: 500},
        placement: 'bottom',
        container: e, 
    })
    

    This is a very simple and awesone solution to this probelm, which i found out by looking into the bootstrap tooltip code. In Bootstrap v3.0.3 here is the line of code i noticed:

    this.options.container ? $tip.appendTo(this.options.container) : $tip.insertAfter(this.$element)
    

    this says that if container property of popover is defined then the popover gets appendTo() the element instead of insertAfter() the original element, all you need to do is just pass the element as container property. Because of appendTo() the popover becomes part of the link on which the hover event was binded and thus keeps the popover open when mouse moves on it.

    0 讨论(0)
  • 2020-12-04 06:41

    This works for me on BootStrap 3:

    el.popover({
      delay: {hide: 100}
    }).on("shown.bs.popover", function(){
      el.data("bs.popover").tip().off("mouseleave").on("mouseleave", function(){
        setTimeout(function(){
          el.popover("hide");
        }, 100);
      });
    }).on("hide.bs.popover", function(ev){
      if(el.data("bs.popover").tip().is(":hover"))
        ev.preventDefault();
    });
    
    0 讨论(0)
提交回复
热议问题