Bootstrap 4 uses the class .collapsing
to animate the width/height of an .collapse-element while opening/closing it. Unfortunately the actual change is approached by adding the width/height as an inline style to the element and adding and removing the class at the start and end of the transition. Therefore it's quite hard to customize the transition (e.g. change timing or fade in/out instead of width transition).
What I've tried so far:
transition:none
to the .collapsing
class: This does help get rid of the transition but opening/closing is still delayed by the transition time, since the class still gets added for a few millis before the actual change takes place..collapsing
class: Since the same class is used for opening and closing, the same animation is shown for both.Is there any way to change the transition e.g. to fade in/out (change of opacity) or do I have to build a custom version of the bootstrap.js?
Just add data-toggle="collapse" and a data-target to the element to automatically assign control of one or more collapsible elements. The data-target attribute accepts a CSS selector to apply the collapse to. Be sure to add the class collapse to the collapsible element.
The collapse JavaScript plugin is used to show and hide content. Buttons or anchors are used as triggers that are mapped to specific elements you toggle. Collapsing an element will animate the height from its current value to 0 . Given how CSS handles animations, you cannot use padding on a .
Generally, we recommend using a button with the data-bs-target attribute. While not recommended from a semantic point of view, you can also use a link with the href attribute (and a role="button" ). In both cases, the data-bs-toggle="collapse" is required.
Bootstrap collapse is used to show or hide content. Buttons or anchors are used to trigger the request and they are mapped to the specific element that needs to be collapsed. In Bootstrap, the collapsing element animates the height of the element from its current height to 0.
If your using SASS to compile bootstrap.css then change the time specified for the $transition-collapse
variable in the _variables.scss
file.
Default setting, for Bootstrap v4.2.1, is on line 254:
$transition-collapse: height .35s ease !default;
I changed .35s to .15s but you can set yours to something your happier with.
It's about the transition style. You can specify it in s for seconds or ms for milliseconds. I have, through experimentation, discovered that this is the minimum html required. Note that Bootstrap uses the id and data-target to associate the button with the section to collapse. So, if you have more than one set of collapsible sections, you'll need separate id's.
<button
data-toggle="collapse"
data-target="#collapseExample">button name</button>
...
<div style="transition: 1s;" id="collapseExample">
your collapsible stuff goes here.
</div>
Additional information:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With