如何取消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:
.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.collapseevent triggers the moment the toggle button is clicked, before Bootstrap starts its transition timer. - By preventing the default behavior, we skip the
.collapsingstate entirely and jump straight to.show. - We update the
aria-expandedattribute to keep the component accessible for screen readers.
内容的提问来源于stack exchange,提问作者Lucian

