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

前端 未结 16 1389
野性不改
野性不改 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: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'});
    

提交回复
热议问题