You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何取消Bootstrap 4中.collapsing后.show类的延迟显示?

Alright, let's tackle this! The default Bootstrap 4 collapse component adds a delay between the .collapsing state and the .show class because it's waiting for the built-in transition to finish. If you want .show to apply the second you click the toggle button, here's how to make that happen:

Solution: Remove .show Class Delay in Bootstrap 4 Collapse

We'll override Bootstrap's default collapse behavior with custom JavaScript to skip the transition delay and apply .show immediately. Here's a step-by-step implementation:

1. Keep Your Existing Markup

First, retain your original navbar and collapse HTML (no changes needed here):

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Features</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Pricing</a>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">Disabled</a>
      </li>
    </ul>
  </div>
</nav>

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>

2. Add Custom JavaScript to Bypass the Delay

Add this script after Bootstrap's JS file to intercept collapse events and apply .show right away:

$(document).ready(function() {
  // Handle the show event (fires immediately when toggle is clicked)
  $('#navbarNav').on('show.bs.collapse', function(e) {
    // Stop Bootstrap's default transition logic
    e.preventDefault();
    // Immediately apply .show and clear any lingering .collapsing class
    $(this).addClass('show').removeClass('collapsing');
    // Update the toggle button's accessibility attribute for compliance
    $('[data-target="#navbarNav"]').attr('aria-expanded', 'true');
  });

  // Handle the hide event similarly (optional, if you want instant close too)
  $('#navbarNav').on('hide.bs.collapse', function(e) {
    e.preventDefault();
    $(this).removeClass('show').removeClass('collapsing');
    $('[data-target="#navbarNav"]').attr('aria-expanded', 'false');
  });
});

3. Optional: Remove Transition CSS (For Instant Visual Change)

If you want to eliminate any faint transition flicker (since we're skipping the .collapsing state), add this CSS to override Bootstrap's default transition:

.collapse.navbar-collapse {
  transition: none !important;
}

Why This Works:

  • The show.bs.collapse event triggers the moment the toggle button is clicked, before Bootstrap starts its transition timer.
  • By preventing the default behavior, we skip the .collapsing state entirely and jump straight to .show.
  • We update the aria-expanded attribute to keep the component accessible for screen readers.

内容的提问来源于stack exchange,提问作者Lucian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:54:31