【问题标题】:Delay closing of bootstrap dropdown延迟关闭引导下拉菜单
【发布时间】:2015-06-08 02:26:08
【问题描述】:

我正在尝试修改引导下拉菜单的行为。首先,我想防止在您单击页面上的其他位置时关闭下拉菜单。我用这段代码做到了:

$('#filters').on({
    "shown.bs.dropdown": function() { this.closable = false; },
    "click":             function() { this.closable = true; },
    "hide.bs.dropdown":  function() { return this.closable; }
});

现在,当您单击其中的链接时,我想延迟关闭下拉菜单。我想这样做是因为在您真正离开页面并加载新页面之前,单击链接时下拉菜单会立即关闭。

这是我目前所拥有的a fiddle

【问题讨论】:

    标签: javascript jquery twitter-bootstrap


    【解决方案1】:

    这个DEMO

    怎么样

    我想建议你两件事:

    • .btn-group 上添加eventlisteners 而不是#filters 作为其 很容易检查哪个dropdown被点击了
    • 将所需的动画添加到 click 事件中

    所以根据上面的建议,代码如下:

    $('.btn-group').on({
        "shown.bs.dropdown": function () {
            this.closable = false;
        },
        "click": function () {
            this.closable = true;
                if(!$(this).hasClass('open'))
                    $(this).find('.dropdown-menu').stop(true, true).slideToggle();
                else
                    $(this).find('.dropdown-menu').stop(true, true).delay(600).slideToggle();
    
        },
        "hide.bs.dropdown": function () {
            return this.closable;
        }
    });
    

    更新:

    根据您的评论,我希望这是您希望实现的目标:

    DEMO

    $('.btn-group').on({
        "shown.bs.dropdown": function () {
            this.closable = false;
        },
            "click": function () {
                this.closable = true;
                $('.btn-group').not($(this)).find('.dropdown-menu').stop(true, true).slideUp();//Close all the dropdowns except this
                if(!$(this).hasClass('open'))
                {
                    $(this).find('.dropdown-menu').stop(true, true).slideToggle();//open only this dropdown
                    $('.btn-group').not($(this)).removeClass('open'); //remove the focus effect from other dropdowns
                }
                else
                    $(this).find('.dropdown-menu').stop(true, true).delay(600).slideToggle();
    
        },
            "hide.bs.dropdown": function () {
            return this.closable;
        }
    });
    

    【讨论】:

    • 谢谢你。您的示例确实完成了延迟,但是当您单击另一个下拉菜单时,它会破坏关闭一个下拉菜单的能力。如果你看看我的小提琴,你会看到当你点击另一个下拉菜单时其他下拉菜单是如何关闭的——但如果你点击页面上的其他地方,它们不会关闭。我想保留这种行为。能做到吗?
    • 是的!这是可以实现的!!给我一点时间!!
    • @Lindsay.. 更新了答案!!请检查!
    • 谢谢!这几乎是正确的,但是当您单击下拉菜单中的链接(如“操作”)时,它会迅速关闭然后再次重新打开。这就是我想添加延迟的部分。我在您的“关闭所有下拉列表”行 (jsfiddle.net/7n7rhtLd/28) 中添加了延迟,但这会增加延迟,我希望延迟仅在您单击下拉列表中的链接时发生。
    • @Lindsay 更新了 DEMO。检查并告诉我!
    【解决方案2】:

    好吧,我认为你必须稍微修改一下正常的引导下拉事件序列,但是只有当 this.closable 为真时,你才能使用带有 if/else 的事件来触发延迟 + slideUp()。然后,由于您无法在 hide.bs.dropdown 事件上“返回 true”,因此您必须删除“open”类,然后在下次单击下拉菜单时自己触发“show()”。 . 像这样:

    这里有一个可用的下拉菜单 - 如果您有多个下拉菜单,您必须为每个下拉菜单创建唯一 ID,然后可能使用 event.target.[something] 来识别正确的下拉菜单以进行切换...

    http://jsfiddle.net/yp44tt9e/2/

    $('#filters').on({
        "show.bs.dropdown": function() {
            console.log($(this));
            $(this).find('.dropdown-menu').show();
        },
        "shown.bs.dropdown": function () {
            this.closable = false;
        },
            "click": function () {
            this.closable = true;
        },
            "hide.bs.dropdown": function (e){
                if(this.closable){
                    e.preventDefault();
                    $(this).find('.dropdown-menu')
                        .first()
                        .stop(true, true)
                        .delay(600)
                        .slideUp('slow', function(){
                            $('.btn-group').removeClass('open');
                        });
                 }else{
                    return this.closable;
                 }
        }
    });
    

    【讨论】:

    • 谢谢。不幸的是,我无法为每个下拉菜单提供唯一的 ID,因为这是在 wordpress 中,并且菜单会经常更改。
    猜你喜欢
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-28
    • 2014-12-15
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    相关资源
    最近更新 更多