首页 > 其他 > 详细

bootstrap的popover在trigger设置为hover时不隐藏popover

时间:2016-08-05 15:36:24      阅读:143      评论:0      收藏:0      [点我收藏+]

使用bootstrap的popover,trigger设置为hover时,可以实现当鼠标放置到目标元素上时显示popover,可是无法实现当鼠标移动到popover上时不隐藏popover,在网上找了下只找到一篇文章(链接),不好的是需要修改bootstrap的源代码,这不是我想要的,只好另寻它路。
后来想到可以在hide.bs.popover事件中使用event.preventDefault()来防止popover关闭,于是就想出了以下方法:

$(".hoverPopover").popover({
        template: ‘<div class="popover" role="tooltip"><div class="arrow"></div><div class="popover-content"></div><h3 class="popover-title" style="border-bottom:none;"></h3></div>‘,
        html: true,
        trigger: "hover",
        placement: "top",
        delay: {hide: 100}
    }).on(‘shown.bs.popover‘, function (event) {
        var that = this;
        $(this).parent().find(‘div.popover‘).on(‘mouseenter‘, function () {
            $(that).attr(‘in‘, true);
        }).on(‘mouseleave‘, function () {
            $(that).removeAttr(‘in‘);
            $(that).popover(‘hide‘);
        });
    }).on(‘hide.bs.popover‘, function (event) {
        if ($(this).attr(‘in‘)) {
            event.preventDefault();
        }
    });

现在把鼠标移动到popover上时,popover不会隐藏了。
重点:

    1. 对popover增加 delay: {hide: 100},让hide事件等待100毫秒再触发;

    2. 在shown.bs.popover事件中为popover元素绑定鼠标事件,在事件中为popover触发元素增加或删除“in”属性;

    3. 在hide.bs.popover事件中检查触发元素是否存在“in”属性,如果存在则取消隐藏。

bootstrap的popover在trigger设置为hover时不隐藏popover

原文:http://www.cnblogs.com/mschen/p/5741165.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!