简体   繁体   English

在悬停时出现Bootstrap Popover / Disappear而不是Click

[英]Make Bootstrap Popover Appear/Disappear on Hover instead of Click

I'm building a website with Bootstrap's Popover and I can't figure out how to make the popover appear on hover instead of click. 我正在使用Bootstrap的Popover构建一个网站,我无法弄清楚如何使鼠标悬停而不是点击。

All I want to do is have a popover appear when someone hovers over a link instead of clicking on it and for the popover to disappear when they move away. 我想要做的就是当有人在链接上盘旋而不是点击它时弹出窗口,当弹出窗口移开时弹出消失。 The documentation says it's possible using either the data attribute or jquery. 文档说明可以使用data属性或jquery。 I'd much rather do it with jquery since I have multiple popovers. 因为我有多个弹出窗口,所以我宁愿用jquery做这件事。

Set the trigger option of the popover to hover instead of click , which is the default one. 将弹出窗口的trigger选项设置为hover而不是click ,这是默认值

This can be done using either data-* attributes in the markup: 这可以使用标记中的data-*属性来完成:

<a id="popover" data-trigger="hover">Popover</a>

Or with an initialization option: 或者使用初始化选项:

$("#popover").popover({ trigger: "hover" });

Here's a DEMO . 这是一个演示

I'd like to add that for accessibility, I think you should add focus trigger : 我想补充一点,为了获取可访问性,我认为你应该添加焦点触发器:

ie $("#popover").popover({ trigger: "hover focus" }); ie $("#popover").popover({ trigger: "hover focus" });

If you want to hover the popover itself as well you have to use a manual trigger. 如果你想悬停弹出窗口本身,你必须使用手动触发器。

This is what i came up with: 这就是我提出的:

function enableThumbPopover() {
    var counter;

    $('.thumbcontainer').popover({
        trigger: 'manual',
        animation: false,
        html: true,
        title: function () {
            return $(this).parent().find('.thumbPopover > .title').html();
        },
        content: function () {
            return $(this).parent().find('.thumbPopover > .body').html();
        },
        container: 'body',
        placement: 'auto'
    }).on("mouseenter",function () {
        var _this = this; // thumbcontainer

        console.log('thumbcontainer mouseenter')
        // clear the counter
        clearTimeout(counter);
        // Close all other Popovers
        $('.thumbcontainer').not(_this).popover('hide');

        // start new timeout to show popover
        counter = setTimeout(function(){
            if($(_this).is(':hover'))
            {
                $(_this).popover("show");
            }
            $(".popover").on("mouseleave", function () {
                $('.thumbcontainer').popover('hide');
            });
        }, 400);

    }).on("mouseleave", function () {
        var _this = this;

        setTimeout(function () {
            if (!$(".popover:hover").length) {
                if(!$(_this).is(':hover')) // change $(this) to $(_this) 
                {
                    $(_this).popover('hide');
                }
            }
        }, 200);
    });
}

The functionality, you described, can be easily achieved using the Bootstrap tooltip. 您描述的功能可以使用Bootstrap工具提示轻松实现。

<button id="example1" data-toggle="tooltip">Tooltip on left</button>

Then call tooltip() function for the element. 然后为元素调用tooltip()函数。

$('#example1').tooltip();

http://getbootstrap.com/javascript/#tooltips http://getbootstrap.com/javascript/#tooltips

After trying a few of these answers and finding they don't scale well with multiple links (for example the accepted answer requires a line of jquery for every link you have), I came across a way that requires minimal code to get working, and it also appears to work perfectly, at least on Chrome. 在尝试了一些这些答案并发现它们不能很好地扩展多个链接之后(例如,接受的答案需要为每个链接提供一行jquery),我遇到了一种需要最少代码才能工作的方法,并且它似乎也很完美,至少在Chrome上。

You add this line to activate it: 您添加此行以激活它:

$('[data-toggle="popover"]').popover();

And these settings to your anchor links: 这些设置到您的锚链接:

data-toggle="popover" data-trigger="hover"

See it in action here , I'm using the same imports as the accepted answer so it should work fine on older projects. 在这里看到它的实际应用 ,我使用与接受的答案相同的导入,因此它应该在旧项目上正常工作。

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

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