Twitter Bootstrap: Call a js function when a dropdown is closed

前端 未结 6 1375
执念已碎
执念已碎 2020-12-13 23:59

Is there an event that I can tie into that is fired when a bootstrap dropdown is closed or toggled?

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-14 00:39

    This is how Bootstrap v2.3.2 closes the menu no matter what you click on:

    $('html').on('click.dropdown.data-api', function () {
        $el.parent().removeClass('open')
    });
    

    If you're using v2.x, this could be used to know when a menu will be closed. However, keep in mind that this is triggered on every click. If you only need to execute something when a menu is really closed (which is probably all the time), you'll need to track when one is opened in the first place. The accepted answer is probably a better solution in that regard.

    In Boostrap v3.0.0, however, the drop menu supports four separate events:

    show.bs.dropdown: This event fires immediately when the show instance method is called.

    shown.bs.dropdown This event is fired when the dropdown has been made visible to the user (will wait for CSS transitions, to complete).

    hide.bs.dropdown This event is fired immediately when the hide instance method has been called.

    hidden.bs.dropdown This event is fired when the dropdown has finished being hidden from the user (will wait for CSS transitions, to complete).

    From Bootstrap's documentation.

提交回复
热议问题